Encuesta Intercensal 2015

La Encuesta Intercensal se guarda en un un archivo .RData.

data <- read_sav("~/Persona.-Encuesta Intercensal 2015.sav")

save(data, 
      file = paste0(here::here(), "/Bases/Encuesta Intercensal_2015.RData"))

Se seleccionan las variables que se desean conservar para la realización de este documento y se guarda en un archivo .RData para practicidad del manejo de datos.

Posibles variables que se pueden contemplar en la movilidad estudiantil

  • SEXO
  • AFRODES
  • HLENGUA
  • QDIALECT_INALI
  • PERTE_INDIGENA
  • ALFABET
  • NIVACAD ¿Cuál fue el último año o grado aprobado por (NOMBRE) en la escuela?
  • TIE_TRASLADO_ESCU
  • MED_TRASLADO_ESC1
  • MED_TRASLADO_ESC2
  • MED_TRASLADO_ESC3
load(paste0(here::here(), "/Bases/Encuesta Intercensal_2015.RData"))

mydata <- data %>%
           select(CVE_ENT, NOM_ENT, MUN, NOM_MUN, ENT_MUN, ENT_PAIS_ASI, MUN_ASI, ENT_MUN_ASI_ESC,
                  EDAD, SEXO, AFRODES,  HLENGUA, QDIALECT_INALI, ELENGUA, PERTE_INDIGENA, ALFABET, 
                  ASISTEN, NIVACAD, ESCOLARI, ESCOACUM, TIE_TRASLADO_ESCU, 
                  MED_TRASLADO_ESC1, MED_TRASLADO_ESC2, MED_TRASLADO_ESC3, FACTOR, ESTRATO, UPM) %>%
            rename("CVE_MUN" = "ENT_MUN",
                   "CVE_MUN_ASI" = "ENT_MUN_ASI_ESC")

save(mydata, file = paste0(here::here(), "/Bases/04_Movilidad estudiantil_2015.RData"))

✔️A partir de aquí se pueden correr los códidos 👇.
Se carga el archivo Movilidad estudiantil_2015.RData.

load(file = paste0(here::here(), "/Bases/04_Movilidad estudiantil_2015.RData"))

#Para fines prácticos se genera un ponderador de uno 
mydata <- mydata %>%
           select(CVE_ENT, NOM_ENT, MUN, CVE_MUN, ENT_PAIS_ASI, MUN_ASI, CVE_MUN_ASI, EDAD, FACTOR, ESTRATO, UPM) %>%
            mutate(M = 1)  %>%
             ungroup()

Entidades

Se genera un vector con el nombre de las entidades llamado estados para facilitar los filtros en el documento.  
Se genera un vector con las abreviaturas de las entidades llamado ent para fines prácticos. 
Se genera un vector con las claves de los municipios.

# Claves de los estados
estados <- sjlabelled::get_labels(mydata$CVE_ENT)

nom_estados <- c( "Aguascalientes", "Baja California" ,"Baja California Sur", "Campeche", "Coahuila de Zaragoza", "Colima", 
                  "Chiapas", "Chihuahua", "Ciudad de México", "Durango", "Guanajuato", "Guerrero", "Hidalgo", "Jalisco",        
                  "México", "Michoacán de Ocampo", "Morelos", "Nayarit", "Nuevo León", "Oaxaca", "Puebla", "Querétaro", 
                  "Quintana Roo", "San Luis Potosí", "Sinaloa", "Sonora", "Tabasco", "Tamaulipas", "Tlaxcala", 
                  "Veracruz de Ignacio de la Llave", "Yucatán", "Zacatecas")

est <- c("AGS", "BC", "BCS", "CAMP", "COAH", "COL", "CHIS", "CHIH", "CDMX", "DGO", "GTO", "GRO", "HGO",
         "JAL", "MEX", "MICH", "MOR", "NAY", "NL", "OAX", "PUE", "QRO", "QROO", "SLP","SIN","SON", "TAB", 
         "TAMS", "TLX", "VER", "YUC", "ZAC")

# Claves de los municipios
MUN <- readRDS(paste0(here::here(), "/Bases/municipios_2015.RDS"))
nom_municipios <- sjlabelled::get_labels(MUN$NOM_MUN) %>% as.factor()
municipios <- sjlabelled::get_labels(MUN$CVE_MUN) %>% as.factor()

# Se le asignan las etiquetas a los nombres de los estados 
levels(mydata$NOM_ENT) <- estados

Población estudiantil de 3 años y más

Se identifica a la población estudiantil de 3 años y más.

filter(EDAD >= 3 & EDAD <= 130)'.

Pob.3ymas <- mydata %>%
              as.data.frame() %>%
               mutate(EDAD = as.numeric(.$EDAD)) %>%
                subset(EDAD >= 3 & EDAD <= 130) %>%
                 filter(ENT_PAIS_ASI %in% estados)  # Filtro del lugar de trabajo dentro del país. 

Población de 3 años y más

Nivel estatal

Movilidad estudiantil

Se utiliza la paquetería survey para poder trabajar con la muestra del cuestionario ampliado, en la cual se selecciona a la población de 3 años y más.

options(survey.lonely.psu = "adjust")

MC <- mydata %>%
       as.data.frame() %>%
        mutate(EDAD = as.numeric(.$EDAD)) %>%
         subset(EDAD >= 3 & EDAD <= 130) %>%  
          filter(ENT_PAIS_ASI %in% estados) %>%
           svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)

saveRDS(MC, file = paste0(here::here(), "/Output/Estado/04_Movilidad estudiantil/2015/MC_estado.RDS"))

Se genera una matriz cruzada de movilidad estudiantil a nivel estatal, utilizando la función svytable de la paquetería survey.

MC <- readRDS(file = paste0(here::here(), "/Output/Estado/04_Movilidad estudiantil/2015/MC_estado.RDS"))

Migrantes <- svytable(~ENT_PAIS_ASI + CVE_ENT, design = MC) 

La función cross_cases() de la paquetería expss se utiliza para crear tablas de contingencia cruzadas a partir de dos o más variables categóricas. Utilizando el comando weight, permite ponderar las observaciones “factores de expansión” en la tabla.

Se quita la diagonal a la matriz cruadrada con la función diag.remove() de la paquetería sna, donde esta función reemplaza los elementos de la diagonal principal de una matriz por un valor nulo o por el valor especifico.

Migrantes <- Migrantes %>%
              as.data.frame() %>%
               expss::cross_cases(CVE_ENT, ENT_PAIS_ASI, weight = Freq) %>%
                as.data.frame() %>%
                 slice(-33) %>% 
                  select(-row_labels) 

rownames(Migrantes)<- nom_estados
colnames(Migrantes) <- nom_estados

save(Migrantes, file = paste0(here::here(), "/Bases/Estado/04_Movilidad estudiantil/2015/Matriz de MEst a nivel estatal 2015.RData"))
 
wb <- createWorkbook()
addWorksheet(wb, "MEstudiantil 2015")
writeData(wb, 1, Migrantes, colNames = TRUE, rowNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Estado/04_Movilidad estudiantil/2015/Matriz de MEst a nivel estatal 2015.xlsx"), overwrite = TRUE)

Matriz de movilidad estudiantil a nivel estatal, 2015

Matriz de movilidad estudiantil, 2015
Nivel estatal
Entidad Aguascalientes Baja California Baja California Sur Campeche Coahuila de Zaragoza Colima Chiapas Chihuahua Ciudad de México Durango Guanajuato Guerrero Hidalgo Jalisco México Michoacán de Ocampo Morelos Nayarit Nuevo León Oaxaca Puebla Querétaro Quintana Roo San Luis Potosí Sinaloa Sonora Tabasco Tamaulipas Tlaxcala Veracruz de Ignacio de la Llave Yucatán Zacatecas
Aguascalientes 387459 0 0 0 0 0 0 0 246 4 226 0 2 419 97 1 0 33 1 9 2 11 0 25 0 11 0 0 3 23 0 739
Baja California 0 907088 0 2 20 0 0 18 202 0 0 0 0 47 4 10 0 0 0 0 34 0 0 0 35 199 0 0 0 1 0 0
Baja California Sur 0 170 186508 0 0 14 0 3 70 0 12 0 0 65 26 0 0 3 31 0 0 0 0 0 30 11 0 0 0 13 0 0
Campeche 0 5 0 246493 6 0 41 0 30 0 0 0 0 2 72 3 0 0 17 13 55 6 417 0 0 17 372 0 0 94 1064 0
Coahuila de Zaragoza 0 0 0 0 782404 8 0 67 364 3856 0 0 0 60 94 0 0 0 1180 8 1 0 0 40 0 0 0 38 0 0 0 40
Colima 0 0 0 0 13 192962 0 0 76 0 0 0 0 825 69 54 5 13 34 0 22 0 0 14 0 0 0 0 0 3 0 1
Chiapas 0 8 0 37 61 0 1524180 13 159 0 16 2 57 32 62 51 15 12 47 39 96 13 30 0 8 3 3502 0 3 96 37 0
Chihuahua 0 6 0 0 168 0 0 959576 247 189 79 0 0 111 11 2 2 6 105 0 32 10 0 3 114 67 1 0 0 23 0 3
Ciudad de México 0 5 0 34 18 51 60 52 2166092 24 47 34 167 133 56756 139 639 0 11 266 1349 243 4 4 0 11 0 53 86 144 173 43
Durango 117 3 0 0 7636 0 0 162 159 483933 14 0 0 70 26 0 3 51 102 0 6 3 0 45 328 0 4 4 0 0 0 196
Guanajuato 86 36 7 0 79 9 68 17 505 3 1591116 5 35 587 282 4081 22 35 23 0 58 5045 0 995 0 13 0 8 14 69 33 45
Guerrero 7 13 9 0 22 37 8 0 1020 0 26 1037434 5 52 1557 1218 1783 5 9 60 783 21 3 15 10 3 2 4 6 108 0 1
Hidalgo 12 7 0 0 23 5 5 19 4491 5 81 3 800580 140 7382 21 22 0 98 2 1282 1291 4 520 1 5 0 108 820 470 0 23
Jalisco 1451 3 0 0 12 1350 13 0 323 0 755 0 3 2159310 173 1060 23 1307 8 0 136 32 40 55 24 21 48 3 6 13 0 841
México 5 36 5 15 38 48 52 33 355896 0 89 502 6637 431 4102576 521 982 3 129 169 1034 1157 10 63 9 14 6 4 361 213 29 66
Michoacán de Ocampo 7 10 2 0 32 339 0 0 508 5 1718 727 11 3300 2240 1218129 69 87 13 0 11 443 3 0 26 0 0 4 0 7 11 4
Morelos 2 0 0 0 54 0 3 0 3455 0 7 154 34 19 1376 13 497096 0 15 0 1600 17 0 1 2 8 0 5 19 19 7 7
Nayarit 0 10 0 0 2 3 0 0 66 28 0 0 0 4611 15 3 0 329134 40 0 0 15 0 0 139 0 0 1 0 16 0 3
Nuevo León 0 7 0 0 294 50 11 0 107 10 15 0 32 70 68 24 8 0 1341619 0 43 0 0 270 16 0 0 278 0 38 0 0
Oaxaca 4 10 0 11 10 7 663 109 797 7 27 392 16 61 464 46 29 1 23 1081067 2623 12 4 6 9 9 109 14 12 4366 31 4
Puebla 6 2 0 5 43 3 8 16 1483 7 14 25 569 26 701 20 431 0 63 351 1782455 63 3 3 3 2 4 6 2776 1789 2 0
Querétaro 0 0 18 0 21 0 11 0 679 3 1119 55 113 44 943 148 73 0 17 16 339 573744 0 330 6 0 4 7 32 79 0 7
Quintana Roo 0 3 0 110 0 0 114 14 106 0 0 35 0 27 111 0 5 0 58 0 78 0 394172 0 0 0 44 20 0 57 1357 0
San Luis Potosí 100 0 2 4 75 2 0 0 150 20 138 3 368 37 105 10 53 7 555 0 22 331 0 798790 0 2 0 982 0 265 0 527
Sinaloa 4 37 54 0 0 0 0 6 118 33 0 29 0 448 45 0 1 322 19 0 19 1 0 33 840167 265 0 2 0 28 8 0
Sonora 0 1654 5 0 11 0 0 53 259 0 11 0 0 112 26 26 6 33 57 6 14 36 2 0 719 771883 0 4 0 94 0 1
Tabasco 0 0 0 323 0 5 3031 0 427 0 0 0 0 0 7 0 6 0 8 0 66 10 12 0 13 0 684275 8 0 365 104 0
Tamaulipas 59 19 0 0 10 0 2 0 183 0 30 1 6 8 74 0 0 0 841 0 48 3 0 123 0 0 23 888434 0 374 0 0
Tlaxcala 2 1 0 0 18 0 0 0 908 3 8 6 522 16 482 2 13 2 7 27 18085 11 0 1 2 0 7 2 344044 83 2 11
Veracruz de Ignacio de la Llave 3 14 2 66 21 23 81 2 1386 3 53 8 1926 78 568 14 28 7 174 1377 5895 89 50 213 14 10 1109 7338 57 2097676 40 0
Yucatán 0 0 0 504 0 0 11 1 89 0 23 0 0 21 30 0 0 0 55 0 2 0 300 0 0 0 17 0 0 31 576199 0
Zacatecas 2033 3 0 0 150 0 9 5 129 259 47 0 9 909 68 15 6 12 64 28 3 22 0 325 14 0 0 10 2 9 0 452789
Fuente: Estimaciones del CONAPO.

Gráfico dinámico

Gráfico dinámico de movilidad estudiantil a nivel estatal.

load(file = paste0(here::here(), "/Bases/Estado/04_Movilidad estudiantil/2015/Matriz de MEst a nivel estatal 2015.RData"))

tabla <- Migrantes %>%
          sna::diag.remove(remove.val = 0) 

names <- c("Aguascalientes", "Baja California" ,"Baja California Sur", "Campeche", "Coahuila", "Colima", 
           "Chiapas", "Chihuahua", "Ciudad de México", "Durango", "Guanajuato", "Guerrero", "Hidalgo", "Jalisco",    
           "México", "Michoacán", "Morelos", "Nayarit", "Nuevo León", "Oaxaca", "Puebla", "Querétaro", 
           "Quintana Roo", "San Luis Potosí", "Sinaloa", "Sonora", "Tabasco", "Tamaulipas", "Tlaxcala", 
           "Veracruz", "Yucatán", "Zacatecas")

# Paleta de colores
paleta <- rev(colorRampPalette(pals::kovesi.linear_bmy_10_95_c71(100))(50))

p <- chorddiag(tabla, 
               groupNames = names,
               groupColors = paleta, 
               groupnamePadding = 10, 
               #height = 700, 
               #width = 700,
               margin = 120,
               groupThickness = 0.07,
               groupPadding = 3,
               groupnameFontsize = 12,
               fadeLevel = '0.1',
               tickInterval = seq(0, 500000, 10000),
               chordedgeColor = "transparent",
               showZeroTooltips = FALSE,
               showTicks = TRUE)

# Ajusta las etiquetas usando JavaScript para modificar su posición
p <- htmlwidgets::onRender(p, '
      function(el, x) {
        d3.selectAll(".group text")
          .attr("text-anchor", "middle")
          .attr("dx", "0")  
          .attr("dy", "0.75em"); 
      }
')

# Crear un contenedor div y aplicar estilos CSS para centrarlo
#p <- tags$div(style = "display: flex; justify-content: center; align-items: center;", p)

p
p %>% 
 mapshot(url = paste0(here::here(),"/images/MEst_2015.html"))

#htmlwidgets::saveWidget(p, paste0(here::here(), "/Graficos/Estado/04_Movilidad estudiantil/2015/MEst a nivel estatal 2015.html"), selfcontained = TRUE)
#require(webshot)
#webshot(url = paste0(here::here(), "/Graficos/Estado/04_Movilidad estudiantil/2015/MEst a nivel estatal 2015.html"),
 #         file = paste0(here::here(), "/Graficos/Estado/04_Movilidad estudiantil/2015/MEst a nivel estatal 2015.png"),
  #                cliprect = "viewport")

Gráficos

ChordDiagram

load(file = paste0(here::here(), "/Bases/Estado/04_Movilidad estudiantil/2015/Matriz de MEst a nivel estatal 2015.RData"))

tabla <- Migrantes %>%
          sna::diag.remove(remove.val = 0)  

rownames(tabla) <- stringr::str_wrap(nom_estados, 100)
colnames(tabla) <- stringr::str_wrap(nom_estados, 100)

# Paleta de colores
#paleta <- colorRampPalette(pals::ocean.matter(100))(50)
paleta <- colorRampPalette(c("#000C7D", "#001599", "#0022B0", "#0035BB", "#004AB4", "#005EA3", "#00708D", "#078472","#3E9A85", "#49A980", "#58B877", "#70C669", "#94D25D", "#BBDA60", "#DDE379", "#DEE53E", "#DBCE33", "#D6B92A", "#D1A521", "#CA911A"))(50)

tabla2 <- color_chord_diagram(tabla1 = tabla, paleta)
file = "/Graficos/Estado/04_Movilidad estudiantil/2015/ChordDiagram de MEst a nivel estatal.pdf"

## Gráficos a nivel estatal 
chord_diagram_graph(file = file, 
                    width = 7, 
                    height = 7, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla, 
                    tabla2 = tabla2, 
                    color_labels = "#000C7D",
                    transparency = 0.4,
                    circo.text = 9,
                    circos.axis.text = 6,
                    adj.text =c(-0.05, 0.5), #Ajuste de las etiquetas (x, y)
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))

Gráficos por estados

Se filtran los flujos migratorios que son exclusivos de los estados y que visualmente sean más interpretables.

load(file = paste0(here::here(), "/Bases/Estado/04_Movilidad estudiantil/2015/Matriz de MEst a nivel estatal 2015.RData"))

tabla <- Migrantes %>%
          sna::diag.remove(remove.val = 0) 

rownames(tabla) <- stringr::str_wrap(nom_estados, 100)
colnames(tabla) <- stringr::str_wrap(nom_estados, 100)

# Nombre de los estados 
estado <- stringr::str_wrap(nom_estados, 100)

filtro_est <- Migrantes %>%
               as.data.frame() %>%
                tibble::rownames_to_column(var = "rn") %>% 
                 melt(., id.vars = "rn", variable.name = "cn") %>%
                  mutate_if(is.factor, as.character) 

### Sacar el promedio de los flujos migratiorios para determinar como se van a grupar los estados 
#### Es importante correr la tabla1[[x]] sin filtros para determinar el número promedio de flujos de migración
### Filtro <<<<  filter(value > 0 & rn != estado[x])
#filtro_mig <- sapply(1:32, function(x)
                      #mean(tail(sort(tabla1[[x]]), 5), na.rm = TRUE))
#p <- data.frame(estados = est,
 #             filtro_estados = filtro_mig)
#write.table(p, file = paste0(here::here(), "/Bases/Estado/04_Movilidad estudiantil/2015/Filtro a nivel estatal.txt"))
#write.xlsx(p, file = paste0(here::here(), "/Bases/Estado/04_Movilidad estudiantil/2015/Filtro a nivel estatal.xlsx"))

filtro_mig <- read.xlsx(paste0(here::here(), "/Bases/Estado/04_Movilidad estudiantil/2015/Filtro a nivel estatal.xlsx"), colNames = TRUE) %>% 
               pull(filtro_estados)

tabla1 <- migration_flows_states(tabla = tabla, 
                                 filtro_mig = filtro_mig, 
                                 filtro_est = filtro_est, 
                                 category = estado, 
                                 group = "Otro estados")

## Se guardan las matrices de movilidad estudiantil para analizarlos después. 
wb <- createWorkbook()
for(i in 1:32){
     tabla <- tabla1[[i]] %>%
                as.data.frame() %>%
                 adorn_totals(c("row", "col"), 
                               fill = "-", 
                                na.rm = TRUE, 
                                 name = "Total",,,,contains(colnames(tabla1[[i]])))
                 
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, tabla, colNames = TRUE, rowNames = TRUE)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Estado/04_Movilidad estudiantil/2015/Matriz MEst por estados_2015_Reduccion.xlsx"), 
               overwrite = TRUE)
}

saveRDS(tabla1, file = paste0(here::here(), "/Output/Estado/04_Movilidad estudiantil/2015/Tabla MEst por estados.RDS"))
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Estado/04_Movilidad estudiantil/2015/Tabla MEst por estados.RDS"))

total_tablas <- totales(tabla1 = tabla1, 
                        Clave = "CVE_ENT", 
                        Inmigrantes = "Salen por estudio",  
                        Emigrantes = "Entran por estudio")

porcentajes_tablas <- porcentajes(tabla1 = tabla1, 
                                  Clave = "CVE_ENT", 
                                  Inmigrantes = "%Salen por estudio",  
                                  Emigrantes = "%Entran por estudio")

# Se guardan los totales de las matrices reducidas 
wb <- createWorkbook()
for(i in 1:32){
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, total_tablas[[i]], colNames = TRUE, startCol = 1)
     writeData(wb, i, porcentajes_tablas[[i]], colNames = TRUE, startCol = 5)
     saveWorkbook(wb, 
                  file = paste0(here::here(),  "/Output/Estado/04_Movilidad estudiantil/2015/Matriz MEst por estados_2015_Reduccion_Totales.xlsx"), 
               overwrite = TRUE)
}
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Estado/04_Movilidad estudiantil/2015/Tabla MEst por estados.RDS"))

# Paleta de colores
#paleta <- rev(colorRampPalette(wesanderson::wes_palette("Rushmore1"))(50)) 
paleta <- colorRampPalette(c("#000C7D", "#001599", "#0022B0", "#0035BB", "#004AB4", "#005EA3", "#00708D", "#078472","#3E9A85", "#49A980", "#58B877", "#70C669", "#94D25D", "#BBDA60", "#DEE53E", "#DBCE33", "#D6B92A", "#D1A521", "#CA911A"))(50)

tabla2 <- color_chord_diagram(tabla1 = tabla1, paleta)
file = "/Graficos/Estado/04_Movilidad estudiantil/2015/ChordDiagram de MEst para cada estado.pdf"

## Gráficos a nivel estatal 
chord_diagram_graph(file = file, 
                    width = 8, 
                    height = 8, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#000C7D",
                    transparency = 0.2,
                    circo.text = 9,
                    circos.axis.text = 6,
                    adj.text =c(-0.05, 0.5), #Ajuste de las etiquetas (x, y)
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))

Etiquetas

file = "/Graficos/Estado/04_Movilidad estudiantil/2015/Etiquetas a nivel estatal.pdf"

labels_chord_diagram(file = file, 
                     width = 7, 
                     height = 8, 
                     family = "Montserrat Medium", 
                     paleta = paleta, 
                     tabla1 = tabla1, 
                     labels = paste(nom_estados))

Gráfico Sankey

Población estudiantil de 3 años y más

Se genera la variable Pob.3ymas sin ningún filtro y se desagrega a nivel estatal para futuros cálculos.

load(file = paste0(here::here(), "/Bases/Estado/04_Movilidad estudiantil/2015/Matriz de MEst a nivel estatal 2015.RData"))

Migrantes <- Migrantes %>%
              sna::diag.remove(remove.val = 0)  

rownames(Migrantes) <- stringr::str_wrap(nom_estados, 50)
colnames(Migrantes) <- stringr::str_wrap(nom_estados, 50)

# Matiz movilidad interna
tabla <- Migrantes %>% 
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             as_tibble() %>%
              mutate(rn = forcats::fct_relevel(.$rn, nom_estados),
                     cn = forcats::fct_relevel(.$cn, nom_estados)) %>%
               filter(value >= 0)  
p <- tabla %>% 
       ggplot(aes(axis1 = rn, 
                   axis2 = cn, 
                    y = value),  # c("value", "freq", "tasa")
               reverse = FALSE, 
                na.rm = TRUE) +
        geom_alluvium(aes(fill = rn),
                       curve_type = "quintic", 
                        color = "transparent", 
                         alpha = 0.85, 
                          lwd = 0.001, 
                           width = 1/5,
                            reverse = FALSE) +
          geom_stratum(aes(fill = cn), 
                        color = "white", 
                         alpha = 0.65,  
                          lwd = 0.001, 
                           width = 1/5,
                            reverse = FALSE) +
           geom_text_repel(aes(label = ifelse(after_stat(x) == 1, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""), 
                               fontface =  ifelse(after_stat(x) == 1, 'bold', 'plain')),
                            stat = "stratum", 
                             size = 3, 
                              direction = "y", 
                               nudge_x = -.2,
                                min.segment.length = unit(1, "lines"),
                                 force = 1,
                                  force_pull = 0,
                                   family = "montserrat",
                                    reverse = FALSE) +
            geom_text_repel(aes(label = ifelse(after_stat(x)  == 2, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                                fontface =  ifelse(after_stat(x) == 2, 'bold', 'plain')),
                             stat = "stratum", 
                              size = 3,
                               direction = "y", 
                                nudge_x = .2, 
                                 force = 1,
                                  force_pull = 0,
                                   family = "montserrat",
                                    reverse = FALSE) +
             theme_void() + 
              theme(plot.margin = margin(t = 1, r = 1.5, b = 1, l = 0, "cm"),
                     text = element_text(family = "montserrat"),
                      axis.text = element_blank(),
                       axis.title = element_blank(),
                        strip.text = element_text(size = 10, face = "bold", family = "montserrat"),
                         legend.key.size = unit(0.5, "cm"),
                          legend.text = element_text(size = 8, family = "montserrat"),
                           legend.position = c(1, .5)) + 
               scale_x_discrete(expand = c(-0.1, 0.35)) +
                scale_fill_viridis_d(option = "A") +
                 guides(fill = guide_legend(ncol = 1, na.translate = F)) + 
                  labs(fill = "", 
                       color = "")

path = paste0(here::here(), "/Graficos/Estado/04_Movilidad estudiantil/2015/GSankey de MEst a nivel estatal.pdf")
ggexport(p, width = 18, height = 10, dpi = 400, filename = path)

Desagregado por estado

load(file = paste0(here::here(), "/Bases/Estado/04_Movilidad estudiantil/2015/Matriz de MEst a nivel estatal 2015.RData"))

rownames(Migrantes) <- estados
colnames(Migrantes) <- estados

tabla <- Migrantes %>%
          sna::diag.remove(remove.val = 0) %>%
           as.data.frame() %>%
            tibble::rownames_to_column(var = "rn") %>% 
             melt(., id.vars = "rn", variable.name = "cn") %>%
              mutate_if(is.factor, as.character) %>%
               mutate(value = ifelse((.$rn != .$cn) & (.$rn %in% estados | .$cn %in% estados), value, 0)) %>%
                mutate(rn = forcats::fct_relevel(.$rn, estados),
                       cn = forcats::fct_relevel(.$cn, estados)) 
p <- lapply(1:32, function(x){
                   tabla <- tabla %>%
                             mutate(rn = forcats::fct_relevel(.$rn, estados),
                                    cn = forcats::fct_relevel(.$cn, estados)) %>%
                              mutate(value = ifelse(.$rn %in% estados[x] | .$cn %in% estados[x], value, 0)) 
 
                    tabla %>% 
                     ggplot(aes(axis1 = rn, 
                                 axis2 = cn, 
                                  y = value),  # c("value", "freq", "tasa")
                             reverse = FALSE, 
                              na.rm = TRUE) + 
                      geom_alluvium(aes(fill = rn),  
                                     color = "transparent", 
                                      alpha = 0.8, 
                                       lwd = 0.001, 
                                        width = 1/5,
                                         reverse = FALSE) +
                       geom_stratum(aes(fill = rn), 
                                     color = "#F1F1F1", 
                                      alpha = 1, 
                                       lwd = 0.001, 
                                        width = 1/5,
                                         reverse = FALSE) +
                         geom_text_repel(aes(label = ifelse(after_stat(x)  == 1, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                                             fontface =  ifelse(after_stat(x) == 1, 'bold', 'plain')),
                                           stat = "stratum", 
                                            size = 3,
                                             direction = "y", 
                                              nudge_x = -.2, 
                                               force = 1,
                                                        force_pull = 0,
                                                         family = "montserrat",
                                                          reverse = FALSE) +
                          geom_text_repel(aes(label = ifelse(after_stat(x)  == 2, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                                              fontface =  ifelse(after_stat(x) == 2, 'bold', 'plain')),
                                           stat = "stratum", 
                                            size = 3,
                                             direction = "y", 
                                              nudge_x = .2, 
                                               force = 1,
                                                force_pull = 0,
                                                 family = "montserrat",
                                                  reverse = FALSE) +
                            theme_void() + 
                             theme(plot.margin = margin(t = 1, r = 1.5, b = 1, l = 0, "cm"),
                                    text = element_text(family = "montserrat"),
                                     axis.text = element_blank(),
                                      axis.title = element_blank(),
                                       strip.text = element_text(size = 10, face = "bold", family = "montserrat"),
                                        legend.key.size = unit(0.5, "cm"),
                                         legend.text = element_text(size = 9, family = "montserrat"),
                                          legend.position = c(1, .5)) + 
                              scale_x_discrete(expand = c(-0.1, 0.35)) +
                               scale_fill_viridis_d(option = "A", end = 0.9, begin = 0.2) +
                                guides(fill = guide_legend(ncol = 1, na.translate = F)) + 
                                 labs(fill = "", 
                                      color = "")
              }
)

path = paste0(here::here(), "/Graficos/Estado/04_Movilidad estudiantil/2015/GSankey de MEst desagregado por estados_Absolutos.pdf")
ggexport(list = p, width = 14, height = 10, dpi = 400, filename = path)

Indicadores

Se realizan cálculos generales de movilidad:

  • Residentes

  • Inmigrantes

  • Emigrantes

  • \(\%\) Inmigrantes

  • \(\%\) %Emigrante

  • Migración bruta

  • Migración Neta

  • \(\%\) Tasa de movilidad bruta

  • \(\%\) Tasa de movilidad neta

Se trabaja con la matriz cuadrada, la cual de esta manera no se satura computacionalmente

################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
              as.data.frame() %>%
               group_by(CVE_ENT) %>%
                summarise(Pob.Total = sum(FACTOR)) 

################################################################################
###################### Pob. estudiantil de 3 años y más ########################
estados <- sjlabelled::get_labels(mydata$CVE_ENT)

Pob.3ymas <- mydata %>%
              as.data.frame() %>%
               mutate(EDAD = as.numeric(.$EDAD)) %>%
                subset((EDAD >= 3 & EDAD <= 130)) %>%
                 #filter(ENT_PAIS_ASI %in% estados) %>%
                  group_by(CVE_ENT) %>%
                   summarise(Pob.3ymas = sum(FACTOR)) 

################################################################################
########################### Residentes #########################################
load(file = paste0(here::here(), "/Bases/Estado/04_Movilidad estudiantil/2015/Matriz de MEst a nivel estatal 2015.RData"))

rownames(Migrantes) <- estados
colnames(Migrantes) <- estados

Residentes <- Migrantes %>%
               rownames_to_column() %>%
                gather(CVE_ENT, Value, -rowname)%>%
                 filter(rowname == CVE_ENT) %>%
                  select(-rowname) %>%
                   droplevels() %>%
                    rename("Residentes" = "Value") 

################################################################################
############################### Inmigrantes ####################################

## Población que sale de su entidad de residencia y entra a otra demarcación por motivos de estudios
Inmigrantes <- Migrantes %>% 
                sna::diag.remove(remove.val = 0) %>%
                 as.data.frame() %>%
                  tibble::rownames_to_column(var = "CVE_ENT") %>%
                   melt(., id.vars = "CVE_ENT", variable.name = "ENT_PAIS_ASI") %>%
                    mutate_at(vars(3), as.numeric) %>%
                     as_tibble() %>%
                      filter(CVE_ENT != ENT_PAIS_ASI) %>%
                       group_by(CVE_ENT) %>%
                        summarise(Inmigrantes = sum(value, na.rm = TRUE))

################################################################################
############################### Emigrantes #####################################

## Población que entra a la entidad para estudiar 
Emigrantes <- Migrantes %>% 
               sna::diag.remove(remove.val = 0) %>%
                as.data.frame() %>%
                 tibble::rownames_to_column(var = "CVE_ENT") %>%
                  melt(., id.vars = "CVE_ENT", variable.name = "ENT_PAIS_ASI") %>%
                   mutate_at(vars(3), as.numeric) %>%
                    as_tibble() %>%
                     filter(CVE_ENT != ENT_PAIS_ASI) %>%
                      group_by(ENT_PAIS_ASI) %>%
                       summarise(Emigrantes = sum(value, na.rm = TRUE)) %>%
                        rename("CVE_ENT" = "ENT_PAIS_ASI")

tabla <- Pob.Total %>%
          left_join(., Pob.3ymas, by = c("CVE_ENT")) %>%
          left_join(., Residentes, by = c("CVE_ENT")) %>%
          left_join(., Inmigrantes, by = c("CVE_ENT")) %>%
          left_join(., Emigrantes, by = c("CVE_ENT")) %>%
            mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
                   Mig.Bruta = .$Inmigrantes + .$Emigrantes, 
                   Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.3ymas) / 2))*1000,
                   Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.3ymas) / 2))*1000,
                   Tasa.Mig = Tasa.Inmig - Tasa.Emig, 
                   Eficacia = Mig.Neta - Mig.Bruta)

write.xlsx(tabla, file = paste0(here::here(), "/Output/Estado/04_Movilidad estudiantil/2015/Indicadores de MEst a nivel estatal 2015.xlsx"), overwrite = TRUE)

save(tabla, file = paste0(here::here(), "/Output/Estado/04_Movilidad estudiantil/2015/Indicadores de MEst a nivel estatal 2015.RData"))
Indicadores de movilidad estudiantil
Nivel estatal
Clave de Entidad Pob.Total Pob.3ymas Residentes Inmigrantes Emigrantes Mig.Neta Mig.Bruta Tasa.Inmig Tasa.Emig Tasa.Mig Eficacia
001 1 312 544 1 237 034 387 459 1 852 3 898 −2 046 5 750 0.3 0.6 −0.3 −7 796
002 3 315 766 3 152 304 907 088 572 2 062 −1 490 2 634 0.0 0.1 −0.1 −4 124
003 712 029 675 676 186 508 448 104 344 552 0.1 0.0 0.1 −208
004 899 931 850 669 246 493 2 214 1 111 1 103 3 325 0.5 0.3 0.3 −2 222
005 2 954 915 2 789 321 782 404 5 756 8 837 −3 081 14 593 0.4 0.6 −0.2 −17 674
006 711 235 674 997 192 962 1 129 1 954 −825 3 083 0.3 0.6 −0.2 −3 908
007 5 217 908 4 872 816 1 524 180 4 399 4 191 208 8 590 0.2 0.2 0.0 −8 382
008 3 556 574 3 371 444 959 576 1 179 590 589 1 769 0.1 0.0 0.0 −1 180
009 8 918 653 8 593 545 2 166 092 60 546 374 638 −314 092 435 184 1.4 8.6 −7.2 −749 276
010 1 754 754 1 652 203 483 933 8 929 4 459 4 470 13 388 1.0 0.5 0.5 −8 918
011 5 853 677 5 530 198 1 591 116 12 160 4 555 7 605 16 715 0.4 0.2 0.3 −9 110
012 3 533 251 3 323 397 1 037 434 6 787 1 981 4 806 8 768 0.4 0.1 0.3 −3 962
013 2 858 359 2 712 770 800 580 16 840 10 512 6 328 27 352 1.2 0.8 0.5 −21 024
014 7 844 830 7 427 770 2 159 310 7 700 12 761 −5 061 20 461 0.2 0.3 −0.1 −25 522
015 16 187 608 15 397 507 4 102 576 368 557 73 934 294 623 442 491 4.7 0.9 3.7 −147 868
016 4 584 471 4 321 980 1 218 129 9 577 7 482 2 095 17 059 0.4 0.3 0.1 −14 964
017 1 903 811 1 810 069 497 096 6 817 4 224 2 593 11 041 0.7 0.5 0.3 −8 448
018 1 181 050 1 113 605 329 134 4 952 1 939 3 013 6 891 0.9 0.3 0.5 −3 878
019 5 119 504 4 860 623 1 341 619 1 341 3 804 −2 463 5 145 0.1 0.2 −0.1 −7 608
020 3 967 889 3 750 461 1 081 067 9 876 2 371 7 505 12 247 0.5 0.1 0.4 −4 742
021 6 168 883 5 827 387 1 782 455 8 424 33 738 −25 314 42 162 0.3 1.1 −0.8 −67 476
022 2 038 372 1 928 438 573 744 4 064 8 885 −4 821 12 949 0.4 0.9 −0.5 −17 770
023 1 501 562 1 420 425 394 172 2 139 882 1 257 3 021 0.3 0.1 0.2 −1 764
024 2 717 820 2 575 847 798 790 3 758 3 084 674 6 842 0.3 0.2 0.1 −6 168
025 2 966 321 2 813 327 840 167 1 472 1 522 −50 2 994 0.1 0.1 0.0 −3 044
026 2 850 330 2 699 733 771 883 3 129 671 2 458 3 800 0.2 0.0 0.2 −1 342
027 2 395 272 2 260 909 684 275 4 385 5 252 −867 9 637 0.4 0.5 −0.1 −10 504
028 3 441 698 3 260 598 888 434 1 804 8 903 −7 099 10 707 0.1 0.5 −0.4 −17 806
029 1 272 847 1 205 179 344 044 20 221 4 197 16 024 24 418 3.3 0.7 2.6 −8 394
030 8 112 505 7 704 417 2 097 676 20 649 8 890 11 759 29 539 0.5 0.2 0.3 −17 780
031 2 097 175 1 992 871 576 199 1 084 2 898 −1 814 3 982 0.1 0.3 −0.2 −5 796
032 1 579 209 1 486 820 452 789 4 131 2 562 1 569 6 693 0.5 0.3 0.2 −5 124
Fuente: Estimaciones del CONAPO.

Nivel municipal

Movilidad estudiantil

Matrices

#Clave de los municipios 2015 
municipios <- MUN %>%
               select(CVE_MUN) %>%
                unique() %>%
                 pull(CVE_MUN)

Pob.3ymas <- mydata %>%
              mutate(CVE_MUN_ASI = paste0(ENT_PAIS_ASI, MUN_ASI)) %>%
               mutate(ENT_PAIS_ASI = case_when(.$ENT_PAIS_ASI %in% estados ~.$ENT_PAIS_ASI,
                                               .$ENT_PAIS_ASI %nin% estados ~ "888", #Residencia en otro país
                                               .$ENT_PAIS_ASI %in% "997" ~ "997",
                                               .$ENT_PAIS_ASI %in% "998" ~ "998",
                                               .$ENT_PAIS_ASI %in% "997" ~ "999"),
                      CVE_MUN_ASI = case_when(.$CVE_MUN_ASI %in% municipios ~.$CVE_MUN_ASI,
                                               nchar(.$CVE_MUN_ASI) == 3 ~ "888",  #Asistencia escolar en otro país
                                              .$CVE_MUN_ASI %in% "997999" ~ '997999',
                                              .$ENT_PAIS_ASI %in% "997" ~ "997",
                                              .$ENT_PAIS_ASI %in% "998" ~ "998",
                                              .$ENT_PAIS_ASI %in% "999" ~ "999",
                                              .$MUN_ASI %in% "999" ~ "999")) %>%
                mutate(EDAD = as.numeric(.$EDAD)) %>%
                 subset(EDAD >= 3 & EDAD <= 130) %>%
                  select(FACTOR, ESTRATO, UPM, CVE_MUN, CVE_MUN_ASI, EDAD) %>%
                   filter(CVE_MUN_ASI %in% municipios) 

MC <- Pob.3ymas %>%
       svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)

saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/MC_municipal.RDS"))
MC <- readRDS(file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/MC_municipal.RDS"))

Migrantes <- svytable(~CVE_MUN_ASI + CVE_MUN, design = MC) 

Se genera la matriz cuadrada y se le asignan los nombres de los estados.

Migrantes <- Migrantes %>%
              as.data.frame() %>%
               expss::cross_cases(CVE_MUN, CVE_MUN_ASI, weight = Freq) %>%
                as.data.frame() %>%
                 rename("CVE_MUN" = "row_labels") %>% 
                  arrange(CVE_MUN) %>%
                   slice(-1) 

rownames <- Migrantes %>% 
             mutate(CVE_MUN = substr(.$CVE_MUN, 9, 16)) %>% 
              pull(CVE_MUN)

colnames <- names(Migrantes) %>% 
             as.data.frame() %>% 
              slice(-1) %>% 
               rename("CVE_MUN" = ".") %>%
                mutate(`CVE_MUN` = substr(.$CVE_MUN, 13, 18)) %>%
                 pull(CVE_MUN)

# Se elimina la variable CVE_MUN
Migrantes <- Migrantes %>%
              select(-CVE_MUN)

rownames(Migrantes) <- rownames 
colnames(Migrantes) <- colnames

saveRDS(Migrantes, file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Matriz de movilidad estudiantil a nivel municipal 2015.RDS"))
save(Migrantes, file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Matriz de movilidad estudiantil a nivel municipal 2015.RData"))

require(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "M.estudiantil")
writeData(wb, 1, Migrantes %>% as.data.frame() %>% tibble::rownames_to_column(var = "CVE_MUN"), colNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Municipio/04_Movilidad estudiantil/2015/Matriz de movilidad estudiantil a nivel municipal 2015.xlsx"), overwrite = TRUE)

Matriz de movilidad estudiantil a nivel municipal, 2015

Matriz de movilidad estudiantil
Nivel municipal
CVE_MUN 001001 001002 001003 001004 001005 001006 001007 001008 001009 001010 001011 002001 002002 002003 002004 002005 003001 003002 003003 003008 003009 004001 004002 004003 004004 004005 004006 004007 004008
001001 255198 64 0 0 911 239 160 52 0 260 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001002 386 12336 0 0 0 90 100 0 15 130 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001003 275 0 14507 0 8 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001004 170 0 0 4350 0 10 249 0 12 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001005 7450 0 52 7 29606 33 132 0 0 8 253 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001006 769 0 0 0 25 12872 426 32 13 0 614 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001007 538 0 0 42 10 205 15734 82 103 0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001008 141 0 0 0 0 69 58 2473 0 0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001009 123 33 0 4 2 104 324 0 5660 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001010 217 24 2 0 0 2 0 0 2 5563 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001011 1404 25 2 3 334 165 146 13 4 3 11843 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
002001 0 0 0 0 0 0 0 0 0 0 0 134903 115 5 25 29 0 0 0 0 0 0 2 0 0 0 0 0 0
002002 0 0 0 0 0 0 0 0 0 0 0 104 272371 0 17 39 0 0 0 0 0 0 0 0 0 0 0 0 0
002003 0 0 0 0 0 0 0 0 0 0 0 10 74 28999 1014 20 0 0 0 0 0 0 0 0 0 0 0 0 0
002004 0 0 0 0 0 0 0 0 0 0 0 241 27 290 439862 2269 0 0 0 0 0 0 0 0 0 0 0 0 0
002005 0 0 0 0 0 0 0 0 0 0 0 38 16 29 1937 24485 0 0 0 0 0 0 0 0 0 0 0 0 0
003001 0 0 0 0 0 0 0 0 0 0 0 16 6 0 0 0 19614 0 153 6 30 0 0 0 0 0 0 0 0
003002 0 0 0 0 0 0 0 0 0 0 0 98 5 0 5 0 0 13157 12 0 8 0 0 0 0 0 0 0 0
003003 0 0 0 0 0 0 0 0 0 0 0 6 2 0 0 0 11 0 73603 97 0 0 0 0 0 0 0 0 0
003008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 27 0 0 0 173 74639 0 0 0 0 0 0 0 0 0
003009 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 40 0 20 2 4866 0 0 0 0 0 0 0 0
004001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 15197 297 24 4 106 16 0 4
004002 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 88 79728 98 78 9 71 0 6
004003 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 45 102 64232 35 0 0 42 0
004004 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 983 80 23452 0 10 0 0
004005 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 210 362 34 2 8117 6 0 22
004006 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 254 0 3 3 9594 0 0
004007 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 33 0 0 0 2373 0
004008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30 213 2 1 51 8 0 2605
004009 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 178 48 53 0 0 0 0
Fuente: Estimaciones del CONAPO.

Gráficos

ChordDiagram

Se filtran los flujos migratorios que son exclusivos de los estados y que visualmente sean más interpretables.

# Matriz cuadrada a nivel municipal 
load(paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Matriz de movilidad estudiantil a nivel municipal 2015.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

# Nombre de los estados 
estado <- stringr::str_wrap(nom_estados, 100)

# Clave de los municipios 
municipios <- MUN %>% 
               mutate(municipios = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                pull(municipios)

################################################################################
################################## Filtro ######################################
Inmigrantes <- Inmigrantes_function(municipios, Migrantes)

Emigrantes <- Emigrantes_function(municipios, Migrantes)   

################################## Filtro ######################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE))

filtro_est <- Migrantes %>%
               as.data.frame() %>%
                tibble::rownames_to_column(var = "rn") %>% 
                 melt(., id.vars = "rn", variable.name = "cn") %>%
                  mutate_if(is.factor, as.character) %>%
                   filter(value > 0)

### Sacar el promedio de los flujos migratiorios para determinar como se van a grupar los estados 
#### Es importante correr la tabla1[[x]] sin filtros para determinar el número promedio de flujos de migración
#### Filtro <<<<< filter(value < 0) %>% 
#p <- data.frame(estados = est,
 #               filtro_municipio = filtro_mig,
  #              filtro_estado = filtro_out)
#write.xlsx(p, file = paste0(here::here(), "/Bases/Municipio/04_Movilidad estudiantil/2015/Filtro a nivel municipal.xlsx"), overwrite = TRUE)

#### Filtro de municipios
filtro_mig <- read.xlsx(paste0(here::here(), "/Bases/Municipio/04_Movilidad estudiantil/2015/Filtro a nivel municipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_municipio)

#### Filtro de estados 
filtro_out <- read.xlsx(paste0(here::here(), "/Bases/Municipio/04_Movilidad estudiantil/2015/Filtro a nivel municipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_estado)

tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character) %>%
              filter(value > 0)

## Se generan los filtros correspondientes a la matriz cuadrada por estados 
tabla1 <- migration_flows_municipality(tabla = tabla, 
                                       filtro_mun = filtro,
                                       filtro_est = filtro_est,
                                       filtro_mig = filtro_mig, 
                                       filtro_out = filtro_out, 
                                       category_group = estados, 
                                       category_names = nom_estados,
                                       group_mun = "Otros municipios",
                                       group_est = "Otros estados")

## Se sacan los flujos migratorios que pertencen a otros estados
#tabla_estados <- sapply(1:32, function(i){
#                                tabla1[[i]] %>%
#                                 as.data.frame() %>%
#                                  adorn_totals(c("row", "col"), 
#                                                fill = "-", 
#                                                 na.rm = TRUE, 
#                                                  ,,,,contains(colnames(tabla1[[i]]))) %>% 
#                                    select(`Otros estados`) %>%
#                                     slice(nrow(.)) %>%
#                                      mutate(`Otros estados` = .$`Otros estados`/30) %>%
#                                       pull(`Otros estados`)
#})

## Se sacan los flujos migratorios que pertencen a otros municipios
#tabla_municipios <- sapply(1:32, function(i){
#                                   tabla1[[i]] %>%
#                                    as.data.frame() %>%
#                                     select(-c(`Otros estados`)) %>%
#                                      adorn_totals(c("row", "col"), 
#                                                    fill = "-", 
#                                                     na.rm = TRUE, 
#                                                      ,,,,contains(colnames(tabla1[[i]]))) %>% 
#                                       slice(nrow(.)) %>%
#                                        mutate(Total = .$Total/50) %>%
#                                        pull(Total)
#})

## Se guardan las matrices de movilidad estudiantil para analizarlos después. 
wb <- createWorkbook()
for(i in 1:32){
     tabla <- tabla1[[i]] %>%
                as.data.frame() %>%
                 adorn_totals(c("row", "col"), 
                               fill = "-", 
                                na.rm = TRUE, 
          ,,,,contains(colnames(tabla1[[i]])))
                 
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, tabla, colNames = TRUE, rowNames = TRUE)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Matriz MEst a nivel municipal_Reduccion.xlsx"), 
               overwrite = TRUE)
}

saveRDS(tabla1, file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Tabla MEst a nivel municipal.RDS"))
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Tabla MEst a nivel municipal.RDS"))

total_tablas <- totales(tabla1 = tabla1, 
                        Clave = "CVE_MUN", 
                        Inmigrantes = "Salen por estudio",  
                        Emigrantes = "Entran por estudio")

porcentajes_tablas <- porcentajes(tabla1 = tabla1, 
                                  Clave = "CVE_MUN", 
                                  Inmigrantes = "%Salen por estudio",  
                                  Emigrantes = "%Entran por estudio")

# Se guardan los totales de las matrices reducidas 
wb <- createWorkbook()
for(i in 1:32){
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, total_tablas[[i]], colNames = TRUE, startCol = 1)
     writeData(wb, i, porcentajes_tablas[[i]], colNames = TRUE, startCol = 5)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Matriz MEst a nivel municipal_Reduccion_Totales.xlsx"), 
               overwrite = TRUE)
}

Dada la magnitud de municipios en algunos estados se seleccionan solo algunos de ellos.

tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Tabla MEst a nivel municipal.RDS"))

# Paleta de colores
#paleta <- rev(colorRampPalette(wesanderson::wes_palette("Rushmore1"))(50)) 
paleta <- colorRampPalette(c("#000C7D", "#001599", "#0022B0", "#0035BB", "#004AB4", "#005EA3", "#00708D", "#078472","#3E9A85", "#49A980", "#58B877", "#70C669", "#94D25D", "#BBDA60", "#DEE53E", "#DBCE33", "#D6B92A", "#D1A521", "#CA911A"))(50)

tabla2 <- color_chord_diagram(tabla1 = tabla1, paleta)
file = "/Graficos/Municipio/04_Movilidad estudiantil/2015/ChordDiagram de MEst desagregado por estado.pdf"

## Gráficos a nivel municipal
chord_diagram_graph(file = file, 
                    width = 15, 
                    height = 10, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#000C7D",
                    transparency = 0.1,
                    circo.text = 9,
                    circos.axis.text = 6,
                    adj.text =c(-0.05, 0.5), #Ajuste de las etiquetas (x, y)
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))

Etiquetas

file = "/Graficos/Municipio/04_Movilidad estudiantil/2015/Etiquetas a nivel municipal.pdf"

labels_chord_diagram(file = file, 
                     width = 7, 
                     height = 10, 
                     family = "Montserrat Medium", 
                     paleta = paleta, 
                     tabla1 = tabla1, 
                     labels = nom_estados)

Indicadores

Se realizan cálculos generales de movilidad:

  • Residentes

  • Inmigrantes

  • Emigrantes

  • \(\%\) Inmigrantes

  • \(\%\) %Emigrante

  • Migración bruta

  • Migración Neta

  • \(\%\) Tasa de movilidad bruta

  • \(\%\) Tasa de movilidad neta

Se trabaja con la matriz cuadrada, la cual de esta manera no se satura computacionalmente

################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
              as.data.frame() %>%
               group_by(CVE_MUN) %>%
                summarise(Pob.Total = sum(FACTOR)) 

################################################################################
###################### Pob. estudiantil de 3 años y más ########################
Pob.3ymas <- mydata %>%
              as.data.frame() %>%
               mutate(EDAD = as.numeric(.$EDAD)) %>%
                subset(EDAD >= 3 & EDAD <= 130) %>%
                 group_by(CVE_MUN) %>%
                  summarise(Pob.3ymas = sum(FACTOR)) 

################################################################################
########################### Residentes #########################################
load(paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Matriz de movilidad estudiantil a nivel municipal 2015.RData"))

Residentes <- Migrantes %>%
               rownames_to_column() %>%
                gather(CVE_MUN, Value ,-rowname)%>%
                 filter(rowname == CVE_MUN) %>%
                  select(-rowname) %>%
                   droplevels() %>%
                    rename("Residentes" = "Value") 

################################################################################
############################### Inmigrantes ####################################
Inmigrantes <- Migrantes %>% 
                as.data.frame() %>%
                 tibble::rownames_to_column(var = "CVE_MUN") %>%
                  melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_ASI") %>%
                   mutate_at(vars(3), as.numeric) %>%
                    as_tibble() %>%
                     filter(CVE_MUN != CVE_MUN_ASI) %>%
                      group_by(CVE_MUN) %>%
                       summarise(Inmigrantes = sum(value, na.rm = TRUE))

################################################################################
############################### Emigrantes #####################################

## Población que entra a la entidad para estudiar 
Emigrantes <- Migrantes %>% 
               as.data.frame() %>%
                tibble::rownames_to_column(var = "CVE_MUN") %>%
                 melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_ASI") %>%
                  mutate_at(vars(3), as.numeric) %>%
                   as_tibble() %>%
                    filter(CVE_MUN != CVE_MUN_ASI) %>%
                     group_by(CVE_MUN_ASI) %>%
                      summarise(Emigrantes = sum(value, na.rm = TRUE)) %>%
                       rename("CVE_MUN" = "CVE_MUN_ASI") 

tabla <- Pob.Total %>%
          left_join(., Pob.3ymas, by = c("CVE_MUN")) %>%
          left_join(., Residentes, by = c("CVE_MUN")) %>%
          left_join(., Inmigrantes, by = c("CVE_MUN")) %>%
          left_join(., Emigrantes, by = c("CVE_MUN")) %>%
            mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
                   Mig.Bruta = .$Inmigrantes + .$Emigrantes, 
                   Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.3ymas) / 2)) * 1000,
                   Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.3ymas) / 2)) * 1000,
                   Tasa.Mig = Tasa.Inmig - Tasa.Emig, 
                   Eficacia = Mig.Neta - Mig.Bruta)

write.xlsx(tabla, file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Indicadores de movilidad estudiantil a nivel municipal 2015.xlsx"), overwrite = TRUE)

save(tabla, file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Indicadores de movilidad estudiantil a nivel municipal 2015.RData"))
Indicadores de movilidad estudiantil
Nivel municipal
CVE_MUN Pob.Total Pob.3ymas Residentes Inmigrantes Emigrantes Mig.Neta Mig.Bruta Tasa.Inmig Tasa.Emig Tasa.Mig Eficacia
001001 877 190 830 111 255 198 2 697 14 454 −11 757 17 151 0.63 3.386 −2.8 −28 908
001002 46 464 43 572 12 336 1 057 404 653 1 461 4.70 1.795 2.9 −808
001003 56 048 52 522 14 507 314 135 179 449 1.16 0.497 0.7 −270
001004 15 577 14 587 4 350 487 214 273 701 6.46 2.838 3.6 −428
001005 120 405 112 668 29 606 7 956 1 290 6 666 9 246 13.65 2.214 11.4 −2 580
001006 46 473 43 452 12 872 1 932 930 1 002 2 862 8.59 4.137 4.5 −1 860
001007 53 866 50 295 15 734 1 047 1 819 −772 2 866 4.02 6.985 −3.0 −3 638
001008 8 896 8 248 2 473 310 181 129 491 7.23 4.223 3.0 −362
001009 20 926 19 549 5 660 614 149 465 763 6.07 1.473 4.6 −298
001010 20 245 18 921 5 563 257 444 −187 701 2.62 4.535 −1.9 −888
001011 46 454 43 109 11 843 2 111 935 1 176 3 046 9.43 4.176 5.3 −1 870
002001 486 639 462 140 134 903 249 674 −425 923 0.10 0.284 −0.2 −1 348
002002 988 417 941 669 272 371 388 1 556 −1 168 1 944 0.08 0.322 −0.2 −3 112
002003 102 406 97 355 28 999 1 118 339 779 1 457 2.24 0.679 1.6 −678
002004 1 641 570 1 559 361 439 862 2 885 3 127 −242 6 012 0.36 0.391 0.0 −6 254
002005 96 734 91 779 24 485 2 024 2 357 −333 4 381 4.29 5.001 −0.7 −4 714
003001 72 564 69 201 19 614 229 51 178 280 0.65 0.144 0.5 −102
003002 60 171 56 803 13 157 139 0 139 139 0.48 0.000 0.5 0
003003 272 711 260 109 73 603 147 441 −294 588 0.11 0.331 −0.2 −882
003008 287 671 271 544 74 639 297 114 183 411 0.21 0.082 0.1 −228
Fuente: Estimaciones del CONAPO.

Movilidad intramunicipal

Matrices

#Clave de los municipios 2015 
municipios <- MUN %>%
               select(CVE_MUN) %>%
                unique() %>%
                 pull(CVE_MUN)

Pob.3ymas <- mydata %>%
              mutate(CVE_MUN_ASI = paste0(ENT_PAIS_ASI, MUN_ASI)) %>%
               mutate(ENT_PAIS_ASI = case_when(.$ENT_PAIS_ASI %in% estados ~.$ENT_PAIS_ASI,
                                               .$ENT_PAIS_ASI %nin% estados ~ "888", #Residencia en otro país
                                               .$ENT_PAIS_ASI %in% "997" ~ "997",
                                               .$ENT_PAIS_ASI %in% "998" ~ "998",
                                               .$ENT_PAIS_ASI %in% "997" ~ "999"),
                      CVE_MUN_ASI = case_when(.$CVE_MUN_ASI %in% municipios ~.$CVE_MUN_ASI,
                                               nchar(.$CVE_MUN_ASI) == 3 ~ "888",  #Asistencia escolar en otro país
                                              .$CVE_MUN_ASI %in% "997999" ~ '997999',
                                              .$ENT_PAIS_ASI %in% "997" ~ "997",
                                              .$ENT_PAIS_ASI %in% "998" ~ "998",
                                              .$ENT_PAIS_ASI %in% "999" ~ "999",
                                              .$MUN_ASI %in% "999" ~ "999")) %>%
                mutate(I_Migracion = case_when(.$CVE_ENT == .$ENT_PAIS_ASI & .$ENT_PAIS_ASI %in% estados ~ 1,
                                               .$CVE_ENT != .$ENT_PAIS_ASI & .$ENT_PAIS_ASI %in% estados ~ 2,
                                               .$ENT_PAIS_ASI %nin% estados ~ 3)) %>%
                 mutate(EDAD = as.numeric(.$EDAD)) %>%
                  subset(EDAD >= 3 & EDAD <= 130) %>%
                   select(FACTOR, ESTRATO, UPM, CVE_MUN, CVE_MUN_ASI, EDAD, I_Migracion) %>%
                    filter(CVE_MUN_ASI %in% municipios & I_Migracion == 1) 

MC <- Pob.3ymas %>%
       svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)

saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/MC_intramunicipal.RDS"))
MC <- readRDS(file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/MC_intramunicipal.RDS"))

Migrantes <- svytable(~CVE_MUN_ASI + CVE_MUN, design = MC) 

Se genera la matriz cuadrada y se le asignan los nombres de los estados.

Migrantes <- Migrantes %>%
              as.data.frame() %>%
               expss::cross_cases(CVE_MUN, CVE_MUN_ASI, weight = Freq) %>%
                as.data.frame() %>%
                 rename("CVE_MUN" = "row_labels") %>% 
                  arrange(CVE_MUN) %>%
                   slice(-1) 

rownames <- Migrantes %>% 
             mutate(CVE_MUN = substr(.$CVE_MUN, 9, 16)) %>% 
              pull(CVE_MUN)

colnames <- names(Migrantes) %>% 
             as.data.frame() %>% 
              slice(-1) %>% 
               rename("CVE_MUN" = ".") %>%
                mutate(`CVE_MUN` = substr(.$CVE_MUN, 13, 18)) %>%
                 pull(CVE_MUN)

# Se elimina la variable CVE_MUN
Migrantes <- Migrantes %>%
              select(-CVE_MUN)

rownames(Migrantes) <- rownames 
colnames(Migrantes) <- colnames

saveRDS(Migrantes, file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Matriz de movilidad estudiantil a nivel intramunicipal 2015.RDS"))
save(Migrantes, file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Matriz de movilidad estudiantil a nivel intramunicipal 2015.RData"))

require(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "M.Intramunicipal")
writeData(wb, 1, Migrantes %>% as.data.frame() %>% tibble::rownames_to_column(var = "CVE_MUN"), colNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Municipio/04_Movilidad estudiantil/2015/Matriz de movilidad estudiantil a nivel intramunicipal 2015.xlsx"), overwrite = TRUE)

Matriz de movilidad estudiantil a nivel municipal, 2015

Matriz de movilidad estudiantil
Nivel intramunicipal
CVE_MUN 001001 001002 001003 001004 001005 001006 001007 001008 001009 001010 001011 002001 002002 002003 002004 002005 003001 003002 003003 003008 003009 004001 004002 004003 004004 004005 004006 004007 004008
001001 255198 64 0 0 911 239 160 52 0 260 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001002 386 12336 0 0 0 90 100 0 15 130 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001003 275 0 14507 0 8 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001004 170 0 0 4350 0 10 249 0 12 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001005 7450 0 52 7 29606 33 132 0 0 8 253 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001006 769 0 0 0 25 12872 426 32 13 0 614 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001007 538 0 0 42 10 205 15734 82 103 0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001008 141 0 0 0 0 69 58 2473 0 0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001009 123 33 0 4 2 104 324 0 5660 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001010 217 24 2 0 0 2 0 0 2 5563 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001011 1404 25 2 3 334 165 146 13 4 3 11843 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
002001 0 0 0 0 0 0 0 0 0 0 0 134903 115 5 25 29 0 0 0 0 0 0 0 0 0 0 0 0 0
002002 0 0 0 0 0 0 0 0 0 0 0 104 272371 0 17 39 0 0 0 0 0 0 0 0 0 0 0 0 0
002003 0 0 0 0 0 0 0 0 0 0 0 10 74 28999 1014 20 0 0 0 0 0 0 0 0 0 0 0 0 0
002004 0 0 0 0 0 0 0 0 0 0 0 241 27 290 439862 2269 0 0 0 0 0 0 0 0 0 0 0 0 0
002005 0 0 0 0 0 0 0 0 0 0 0 38 16 29 1937 24485 0 0 0 0 0 0 0 0 0 0 0 0 0
003001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19614 0 153 6 30 0 0 0 0 0 0 0 0
003002 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13157 12 0 8 0 0 0 0 0 0 0 0
003003 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 0 73603 97 0 0 0 0 0 0 0 0 0
003008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 173 74639 0 0 0 0 0 0 0 0 0
003009 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 40 0 20 2 4866 0 0 0 0 0 0 0 0
004001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 15197 297 24 4 106 16 0 4
004002 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 88 79728 98 78 9 71 0 6
004003 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 45 102 64232 35 0 0 42 0
004004 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 983 80 23452 0 10 0 0
004005 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 210 362 34 2 8117 6 0 22
004006 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 254 0 3 3 9594 0 0
004007 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 33 0 0 0 2373 0
004008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30 213 2 1 51 8 0 2605
004009 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 178 48 53 0 0 0 0
Fuente: Estimaciones del CONAPO.

Gráficos

ChordDiagram

Se filtran los flujos migratorios que son exclusivos de los estados y que visualmente sean más interpretables.

# Matriz cuadrada a nivel municipal 
load(paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Matriz de movilidad estudiantil a nivel intramunicipal 2015.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

# Nombre de los estados 
estado <- stringr::str_wrap(nom_estados, 100)

# Clave de los municipios 
municipios <- MUN %>% 
               mutate(municipios = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                pull(municipios)

################################################################################
################################## Filtro ######################################
Inmigrantes <- Inmigrantes_function(municipios, Migrantes)

Emigrantes <- Emigrantes_function(municipios, Migrantes)

### Sacar el promedio de los flujos migratiorios para determinar como se van a grupar los estados 
#### Es importante correr la tabla1[[x]] sin filtros para determinar el número promedio de flujos de migración
#### Filtro <<<<< filter(value < 0) %>% 
#p <- data.frame(estados = est,
 #               filtro_municipio = filtro_mig)
#write.table(p, file = paste0(here::here(), "/Bases/Municipio/04_Movilidad estudiantil/2015/Filtro a nivel intramunicipal.txt"), col.names = TRUE)
#write.xlsx(p, file = paste0(here::here(), "/Bases/Municipio/04_Movilidad estudiantil/2015/Filtro a nivel intramunicipal.xlsx"), overwrite = TRUE)

#### Filtro de municipios
filtro_mig <- read.xlsx(paste0(here::here(), "/Bases/Municipio/04_Movilidad estudiantil/2015/Filtro a nivel intramunicipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_municipio)

################################################################################
## Se generan los filtros correspondientes a la matriz cuadrada por estados 
tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character) %>%
              filter(value > 0)

tabla1 <- migration_flows_municipality(tabla = tabla, 
                                       filtro_mun = filtro,
                                       filtro_est = NULL,
                                       filtro_mig = filtro_mig, 
                                       filtro_out = NULL,
                                       category_group = estados, 
                                       category_names = nom_estados,
                                       group_mun = "Otros municipios",
                                       group_est = NULL)

################################################################################
#tabla_municipios <- sapply(1:32, function(x)
#                                   tabla1[[x]] %>%
#                                     as.data.frame() %>%
#                                      adorn_totals(c("col"), 
#                                                    fill = "-", 
#                                                     na.rm = TRUE, 
#                                                      ,,,,contains(rownames(tabla1[[x]]))) %>%
#                                        select(Total) %>% 
#                                         summarise(mean = mean(Total)) %>%
#                                          pull(mean))

## Se guardan las matrices de movilidad estudiantil para analizarlos después. 
wb <- createWorkbook()
for(i in 1:32){
         tabla <- tabla1[[i]] %>%
                    as.data.frame() %>%
                     adorn_totals(c("row", "col"), 
                                   fill = "-", 
                                    na.rm = TRUE, 
              ,,,,contains(colnames(tabla1[[i]])))
                     
         addWorksheet(wb, paste(est[i]))
         writeData(wb, i, tabla, colNames = TRUE, rowNames = TRUE)
         saveWorkbook(wb, 
                      file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Matriz MEst nivel intramunicipal_Reduccion.xlsx"), 
                   overwrite = TRUE)
}

saveRDS(tabla1, file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Tabla MEst a nivel intramunicipal.RDS"))
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Tabla MEst a nivel intramunicipal.RDS"))

total_tablas <- totales(tabla1 = tabla1, 
                        Clave = "CVE_MUN", 
                        Inmigrantes = "Salen por estudio",  
                        Emigrantes = "Entran por estudio")

porcentajes_tablas <- porcentajes(tabla1 = tabla1, 
                                  Clave = "CVE_MUN", 
                                  Inmigrantes = "%Salen por estudio",  
                                  Emigrantes = "%Entran por estudio")

# Se guardan los totales de las matrices reducidas 
wb <- createWorkbook()
for(i in 1:32){
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, total_tablas[[i]], colNames = TRUE, startCol = 1)
     writeData(wb, i, porcentajes_tablas[[i]], colNames = TRUE, startCol = 5)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Matriz MEst nivel intramunicipal_Reduccion_Totales.xlsx"), 
               overwrite = TRUE)
}

Dada la magnitud de municipios en algunos estados se seleccionan solo algunos de ellos.

tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Tabla MEst a nivel intramunicipal.RDS"))

# Paleta de colores
#paleta <- rev(colorRampPalette(wesanderson::wes_palette("Rushmore1"))(50)) 
paleta <- colorRampPalette(c("#000C7D", "#001599", "#0022B0", "#0035BB", "#004AB4", "#005EA3", "#00708D", "#078472","#3E9A85", "#49A980", "#58B877", "#70C669", "#94D25D", "#BBDA60", "#DEE53E", "#DBCE33", "#D6B92A", "#D1A521", "#CA911A"))(50)

tabla2 <- color_chord_diagram(tabla1 = tabla1, paleta)
file = "/Graficos/Municipio/04_Movilidad estudiantil/2015/ChordDiagram de MEst desagregado a nivel intramunicipal.pdf"

## Gráficos a nivel intramunicipal
chord_diagram_graph(file = file, 
                    width = 15, 
                    height = 10, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#000C7D",
                    transparency = 0,
                    circo.text = 9,
                    circos.axis.text = 6,
                    adj.text =c(-0.05, 0.5), #Ajuste de las etiquetas (x, y)
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))

Etiquetas

file = "/Graficos/Municipio/04_Movilidad estudiantil/2015/Etiquetas a nivel intramunicipal.pdf"

labels_chord_diagram(file = file, 
                     width = 7, 
                     height = 8, 
                     family = "Montserrat Medium", 
                     paleta = paleta, 
                     tabla1 = tabla1, 
                     labels = nom_estados)

Gráfico Sankey

# Matriz cuadrada a nivel municipal 
load(paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Matriz de movilidad estudiantil a nivel intramunicipal 2015.RData"))

tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character)

################################################################################
################################## Filtro ######################################
Inmigrantes  <- Migrantes %>%
                 as.data.frame() %>%
                  tibble::rownames_to_column(var = "rn") %>% 
                   melt(., id.vars = "rn", variable.name = "cn") %>%
                    mutate_if(is.factor, as.character) %>%
                     mutate(value = ifelse((.$rn != .$cn) & (substr(.$rn, 1, 3) %in% estados | substr(.$cn, 1, 3) %in% estados), value, 0)) %>%
                      filter(value > 0) %>%
                       group_by(rn) %>% 
                        summarise(Inmigrantes = sum(value, na.rm = TRUE)) 

Emigrantes <- Migrantes %>%
               as.data.frame() %>%
                tibble::rownames_to_column(var = "rn") %>% 
                 melt(., id.vars = "rn", variable.name = "cn") %>%
                  mutate_if(is.factor, as.character) %>%
                   mutate(value = ifelse((.$rn != .$cn) & (substr(.$rn, 1, 3) %in% estados | substr(.$cn, 1, 3) %in% estados), value, 0)) %>%
                    filter(value > 0) %>%
                     group_by(cn) %>% 
                      summarise(Emigrantes = sum(value, na.rm = TRUE))     

################################## Filtro ######################################
# Se utiliza el filtro de arriba
################################################################################

tabla1 <- lapply(1:32, function(x){
                   filtro  <- Inmigrantes %>%
                                full_join(., Emigrantes, by = c("rn" = "cn")) %>%
                                 mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE)) %>%
                                  filter(value < filtro_mig[x]) %>% 
                                   pull(rn)
                   tabla %>%
                    mutate(rn = case_when(substr(.$rn, 1, 3) %in% estados[x] & .$rn %in% filtro ~ paste0("Otros municipios (", estados[x], ")"),
                                          substr(.$rn, 1, 3) %in% estados[x]  & .$rn %nin% filtro ~ .$rn,
                                          substr(.$rn, 1, 3) %nin% estados[x] ~ substr(.$rn, 1, 3)),
                           cn = case_when(substr(.$cn, 1, 3) %in% estados[x] & .$cn %in% filtro ~ paste0("Otros municipios (", estados[x], ")"),
                                          substr(.$cn, 1, 3) %in% estados[x] & .$cn %nin% filtro ~ .$cn,
                                          substr(.$cn, 1, 3) %nin% estados[x] ~ substr(.$cn, 1, 3))) %>%

                     mutate(value = ifelse(.$rn != .$cn, .$value, 0)) %>%
                      filter(value > 0) 
  }
)
p <- lapply(1:32, function(x){
             tabla1[[x]] %>% 
              ggplot(aes(axis1 = rn, 
                          axis2 = cn, 
                           y = value),  # c("value", "freq", "tasa")
                      reverse = FALSE, 
                       na.rm = TRUE) +
               geom_alluvium(aes(fill = rn),
                              curve_type = "quintic", 
                               color = "transparent", 
                                alpha = 0.85, 
                                 lwd = 0.001, 
                                  width = 1/5,
                                   reverse = FALSE) +
                 geom_stratum(aes(fill = cn), 
                               color = "white", 
                                alpha = 0.65,  
                                 lwd = 0.001, 
                                  width = 1/5,
                                   reverse = FALSE) +
                  geom_text_repel(aes(label = ifelse(after_stat(x) == 1, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""), 
                                       fontface =  ifelse(after_stat(x) == 1, 'bold', 'plain')),
                                   stat = "stratum", 
                                    size = 3, 
                                     direction = "y", 
                                      nudge_x = -.2,
                                       min.segment.length = unit(1, "lines"),
                                        force = 1,
                                         force_pull = 0,
                                          family = "montserrat",
                                           reverse = FALSE) +
                   geom_text_repel(aes(label = ifelse(after_stat(x)  == 2, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                                        fontface =  ifelse(after_stat(x) == 2, 'bold', 'plain')),
                                    stat = "stratum", 
                                     size = 3,
                                      direction = "y", 
                                       nudge_x = .2, 
                                        force = 1,
                                         force_pull = 0,
                                          family = "montserrat",
                                           reverse = FALSE) +
                     theme_void() + 
                      theme(plot.margin = margin(t = 1, r = 5, b = 1, l = 0, "cm"),
                             text = element_text(family = "montserrat"),
                              axis.text = element_blank(),
                               axis.title = element_blank(),
                                strip.text = element_text(size = 10, face = "bold", family = "montserrat"),
                                 legend.key.size = unit(0.5, "cm"),
                                  legend.text = element_text(size = 7, family = "montserrat"),
                                   legend.position = c(1.02, .5)) + 
                       scale_x_discrete(expand = c(-0.1, 0.5)) +
                        scale_fill_viridis_d(option = "A", end = 1, begin = 0.2) +
                         guides(fill = guide_legend(ncol = 1, na.translate = F)) + 
                          labs(fill = "", 
                               color = "")
  }
)

path = paste0(here::here(), "/Graficos/Municipio/04_Movilidad estudiantil/2015/GSankey de MEst desagregado a nivel intramunicipal.pdf")
ggexport(list = p, width = 18, height = 10, dpi = 400, filename = path)

Indicadores

Se realizan cálculos generales de movilidad:

  • Residentes

  • Inmigrantes

  • Emigrantes

  • \(\%\) Inmigrantes

  • \(\%\) %Emigrante

  • Migración bruta

  • Migración Neta

  • \(\%\) Tasa de movilidad bruta

  • \(\%\) Tasa de movilidad neta

Se trabaja con la matriz cuadrada, la cual de esta manera no se satura computacionalmente

################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
              as.data.frame() %>%
               group_by(CVE_MUN) %>%
                summarise(Pob.Total = sum(FACTOR)) 

################################################################################
###################### Pob. estudiantil de 3 años y más ########################
Pob.3ymas <- mydata %>%
              as.data.frame() %>%
               mutate(EDAD = as.numeric(.$EDAD)) %>%
                subset(EDAD >= 3 & EDAD <= 130) %>%
                 group_by(CVE_MUN) %>%
                  summarise(Pob.3ymas = sum(FACTOR)) 

################################################################################
########################### Residentes #########################################
load(paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Matriz de movilidad estudiantil a nivel intramunicipal 2015.RData"))

Residentes <- Migrantes %>%
               rownames_to_column() %>%
                gather(CVE_MUN, Value ,-rowname)%>%
                 filter(rowname == CVE_MUN) %>%
                  select(-rowname) %>%
                   droplevels() %>%
                    rename("Residentes" = "Value") 

################################################################################
############################### Inmigrantes ####################################

## Población que sale de su entidad de residencia y entra a otro demarcación por motivos de estudios
Inmigrantes <- Migrantes %>% 
                as.data.frame() %>%
                 tibble::rownames_to_column(var = "CVE_MUN") %>%
                  melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_ASI") %>%
                   mutate_at(vars(3), as.numeric) %>%
                    as_tibble() %>%
                     filter(CVE_MUN != CVE_MUN_ASI) %>%
                      group_by(CVE_MUN) %>%
                       summarise(Inmigrantes = sum(value, na.rm = TRUE))

################################################################################
############################### Emigrantes #####################################

## Población que entra a la entidad para estudiar 
Emigrantes <- Migrantes %>% 
               as.data.frame() %>%
                tibble::rownames_to_column(var = "CVE_MUN") %>%
                 melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_ASI") %>%
                  mutate_at(vars(3), as.numeric) %>%
                   as_tibble() %>%
                    filter(CVE_MUN != CVE_MUN_ASI) %>%
                     group_by(CVE_MUN_ASI) %>%
                      summarise(Emigrantes = sum(value, na.rm = TRUE)) %>%
                       rename("CVE_MUN" = "CVE_MUN_ASI") 

tabla <- Pob.Total %>%
          left_join(., Pob.3ymas, by = c("CVE_MUN")) %>%
          left_join(., Residentes, by = c("CVE_MUN")) %>%
          left_join(., Inmigrantes, by = c("CVE_MUN")) %>%
          left_join(., Emigrantes, by = c("CVE_MUN")) %>%
            mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
                   Mig.Bruta = .$Inmigrantes + .$Emigrantes, 
                   Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.3ymas) / 2)) * 1000,
                   Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.3ymas) / 2)) * 1000,
                   Tasa.Mig = Tasa.Inmig - Tasa.Emig, 
                   Eficacia = Mig.Neta - Mig.Bruta)

write.xlsx(tabla, file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Indicadores de movilidad estudiantil a nivel intramunicipal 2015.xlsx"), overwrite = TRUE)

save(tabla, file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Indicadores de movilidad estudiantil a nivel intramunicipal 2015.RData"))
Indicadores de movilidad estudiantil
Nivel intramunicipal
CVE_MUN Pob.Total Pob.3ymas Residentes Inmigrantes Emigrantes Mig.Neta Mig.Bruta Tasa.Inmig Tasa.Emig Tasa.Mig Eficacia
001001 877 190 830 111 255 198 1 709 11 473 −9 764 13 182 0.400 2.688 −2.3 −22 946
001002 46 464 43 572 12 336 725 146 579 871 3.221 0.649 2.6 −292
001003 56 048 52 522 14 507 286 56 230 342 1.054 0.206 0.8 −112
001004 15 577 14 587 4 350 445 56 389 501 5.901 0.743 5.2 −112
001005 120 405 112 668 29 606 7 935 1 290 6 645 9 225 13.618 2.214 11.4 −2 580
001006 46 473 43 452 12 872 1 879 917 962 2 796 8.358 4.079 4.3 −1 834
001007 53 866 50 295 15 734 995 1 595 −600 2 590 3.821 6.125 −2.3 −3 190
001008 8 896 8 248 2 473 283 179 104 462 6.603 4.176 2.4 −358
001009 20 926 19 549 5 660 599 149 450 748 5.920 1.473 4.4 −298
001010 20 245 18 921 5 563 247 406 −159 653 2.523 4.146 −1.6 −812
001011 46 454 43 109 11 843 2 099 935 1 164 3 034 9.374 4.176 5.2 −1 870
002001 486 639 462 140 134 903 174 393 −219 567 0.073 0.166 −0.1 −786
002002 988 417 941 669 272 371 160 232 −72 392 0.033 0.048 0.0 −464
002003 102 406 97 355 28 999 1 118 324 794 1 442 2.239 0.649 1.6 −648
002004 1 641 570 1 559 361 439 862 2 827 2 993 −166 5 820 0.353 0.374 0.0 −5 986
002005 96 734 91 779 24 485 2 020 2 357 −337 4 377 4.286 5.001 −0.7 −4 714
003001 72 564 69 201 19 614 189 51 138 240 0.533 0.144 0.4 −102
003002 60 171 56 803 13 157 20 0 20 20 0.068 0.000 0.1 0
003003 272 711 260 109 73 603 108 358 −250 466 0.081 0.269 −0.2 −716
003008 287 671 271 544 74 639 173 105 68 278 0.124 0.075 0.0 −210
Fuente: Estimaciones del CONAPO.

Movilidad intermunicipal

Matrices

#Clave de los municipios 2015 
municipios <- MUN %>%
               select(CVE_MUN) %>%
                unique() %>%
                 pull(CVE_MUN)

Pob.3ymas <- mydata %>%
              mutate(CVE_MUN_ASI = paste0(ENT_PAIS_ASI, MUN_ASI)) %>%
               mutate(ENT_PAIS_ASI = case_when(.$ENT_PAIS_ASI %in% estados ~.$ENT_PAIS_ASI,
                                               .$ENT_PAIS_ASI %nin% estados ~ "888", #Residencia en otro país
                                               .$ENT_PAIS_ASI %in% "997" ~ "997",
                                               .$ENT_PAIS_ASI %in% "998" ~ "998",
                                               .$ENT_PAIS_ASI %in% "997" ~ "999"),
                      CVE_MUN_ASI = case_when(.$CVE_MUN_ASI %in% municipios ~.$CVE_MUN_ASI,
                                               nchar(.$CVE_MUN_ASI) == 3 ~ "888",  #Asistencia escolar en otro país
                                              .$CVE_MUN_ASI %in% "997999" ~ '997999',
                                              .$ENT_PAIS_ASI %in% "997" ~ "997",
                                              .$ENT_PAIS_ASI %in% "998" ~ "998",
                                              .$ENT_PAIS_ASI %in% "999" ~ "999",
                                              .$MUN_ASI %in% "999" ~ "999")) %>%
                mutate(I_Migracion = case_when(.$CVE_ENT == .$ENT_PAIS_ASI & .$ENT_PAIS_ASI %in% estados ~ 1,
                                               .$CVE_ENT != .$ENT_PAIS_ASI & .$ENT_PAIS_ASI %in% estados ~ 2,
                                               .$ENT_PAIS_ASI %nin% estados ~ 3)) %>%
                 mutate(EDAD = as.numeric(.$EDAD)) %>%
                  subset(EDAD >= 3 & EDAD <= 130) %>%
                   select(FACTOR, ESTRATO, UPM, CVE_MUN, CVE_MUN_ASI, EDAD, I_Migracion) %>%
                    filter(CVE_MUN_ASI %in% municipios & I_Migracion == 2) 

MC <- Pob.3ymas %>%
       svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)

saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/MC_intermunicipal.RDS"))
MC <- readRDS(file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/MC_intermunicipal.RDS"))

Migrantes <- svytable(~CVE_MUN_ASI + CVE_MUN, design = MC) 

Se genera la matriz cuadrada y se le asignan los nombres de los estados.

Migrantes <- Migrantes %>%
              as.data.frame() %>%
               expss::cross_cases(CVE_MUN, CVE_MUN_ASI, weight = Freq) %>%
                as.data.frame() %>%
                 rename("CVE_MUN" = "row_labels") %>% 
                  arrange(CVE_MUN) %>%
                   slice(-1) 

rownames <- Migrantes %>% 
             mutate(CVE_MUN = substr(.$CVE_MUN, 9, 16)) %>% 
              pull(CVE_MUN)

colnames <- names(Migrantes) %>% 
             as.data.frame() %>% 
              slice(-1) %>% 
               rename("CVE_MUN" = ".") %>%
                mutate(`CVE_MUN` = substr(.$CVE_MUN, 13, 18)) %>%
                 pull(CVE_MUN)

# Se elimina la variable CVE_MUN
Migrantes <- Migrantes %>%
              select(-CVE_MUN)

rownames(Migrantes) <- rownames 
colnames(Migrantes) <- colnames

saveRDS(Migrantes, file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Matriz de movilidad estudiantil a nivel intermunicipal 2015.RDS"))
save(Migrantes, file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Matriz de movilidad estudiantil a nivel intermunicipal 2015.RData"))

require(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "M.Intermunicipal")
writeData(wb, 1, Migrantes %>% as.data.frame() %>% tibble::rownames_to_column(var = "CVE_MUN"), colNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Municipio/04_Movilidad estudiantil/2015/Matriz de movilidad estudiantil a nivel intermunicipal 2015.xlsx"), overwrite = TRUE)

Matriz de movilidad estudiantil a nivel municipal, 2015

Matriz de movilidad estudiantil
Nivel intermunicipal
CVE_MUN 001001 001002 001003 001004 001006 001007 001008 001010 002001 002002 002003 002004 003003 003008 004001 004002 004003 004004 004005 004007 004009 004010 004011 005003 005004 005006 005013 005017 005018
001001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001002 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001003 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001004 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001005 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001006 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001007 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001009 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001010 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001011 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
002001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0
002002 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
002004 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
002005 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
003001 0 0 0 0 0 0 0 0 16 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
003002 0 0 0 0 0 0 0 0 98 5 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
003003 0 0 0 0 0 0 0 0 6 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
003008 0 0 0 0 0 0 0 0 0 0 0 27 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
003009 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004002 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004003 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004004 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004005 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004006 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004007 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004009 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004010 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Fuente: Estimaciones del CONAPO.

Gráficos

ChordDiagram

Se filtran los flujos migratorios que son exclusivos de los estados y que visualmente sean más interpretables.

# Matriz cuadrada a nivel municipal 
load(paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Matriz de movilidad estudiantil a nivel intermunicipal 2015.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

# Nombre de los estados 
estado <- stringr::str_wrap(nom_estados, 100)

# Clave de los municipios 
municipios <- MUN %>% 
               mutate(municipios = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                pull(municipios)
             
################################################################################
################################## Filtro ######################################
Inmigrantes <- Inmigrantes_function(municipios, Migrantes)

Emigrantes <- Emigrantes_function(municipios, Migrantes)  

################################## Filtro ######################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE)) %>%
              filter(value > 0)

filtro_est <- Migrantes %>%
               as.data.frame() %>%
                tibble::rownames_to_column(var = "rn") %>% 
                 melt(., id.vars = "rn", variable.name = "cn") %>%
                  mutate_if(is.factor, as.character) %>%
                   filter(value > 0)

### Sacar el promedio de los flujos migratiorios para determinar como se van a grupar los estados 
#### Es importante correr la tabla1[[x]] sin filtros para determinar el número promedio de flujos de migración
#### Filtro <<<<< filter(value < 0) %>%
#### Filtro de estados filter(value > 100000000000 & rn != estados[x]) %>%
#### Se mete el filtro dentro de la función tabla1[[x]]
#p <- data.frame(estados = est,
 #               filtro_municipio = filtro_mig,
  #              filtro_estado = filtro_out)
#write.table(p, file = paste0(here::here(), "/Bases/Municipio/04_Movilidad estudiantil/2015/Filtro a nivel intermunicipal.txt"), col.names = TRUE)
#write.xlsx(p, file = paste0(here::here(), "/Bases/Municipio/04_Movilidad estudiantil/2015/Filtro a nivel intermunicipal.xlsx"), overwrite = TRUE)

#### Filtro de municipios
filtro_mig <- read.xlsx(paste0(here::here(), "/Bases/Municipio/04_Movilidad estudiantil/2015/Filtro a nivel intermunicipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_municipio)

#### Filtro de estados 
filtro_out <- read.xlsx(paste0(here::here(), "/Bases/Municipio/04_Movilidad estudiantil/2015/Filtro a nivel intermunicipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_estado)

################################################################################
## Se generan los filtros correspondientes a la matriz cuadrada por estados 
tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character) %>% 
              filter(value > 0)

tabla1 <- migration_flows_municipality(tabla = tabla, 
                                       filtro_mun = filtro,
                                       filtro_est = filtro_est,
                                       filtro_mig = filtro_mig, 
                                       filtro_out = filtro_out, 
                                       category_group = estados, 
                                       category_names = nom_estados,
                                       group_mun = "Otros municipios",
                                       group_est = "Otros estados")

################################################################################
## Se sacan los flujos migratorios que pertencen a otros estados
#tabla_estados <- sapply(1:32, function(i){
#                                tabla1[[i]] %>%
#                                 as.data.frame() %>%
#                                  adorn_totals(c("row", "col"), 
#                                               fill = "-", 
#                                                na.rm = TRUE, 
#                                                 ,,,,contains(colnames(tabla1[[i]]))) %>% 
#                                   select(`Otros estados`) %>%
#                                    slice(nrow(.)) %>%
#                                     mutate(`Otros estados` = .$`Otros estados`/10) %>%
#                                      pull(`Otros estados`)
#})

## Se sacan los flujos migratorios que pertencen a otros municipios
#tabla_municipios <- sapply(1:32, function(i){
#                                  tabla1[[i]] %>%
#                                   as.data.frame() %>%
#                                    select(-c(`Otros estados`)) %>%
#                                     adorn_totals(c("row", "col"), 
#                                                   fill = "-", 
#                                                    na.rm = TRUE, 
#                                                     ,,,,contains(colnames(tabla1[[i]]))) %>% 
#                                      slice(nrow(.)) %>%
#                                       mutate(Total = .$Total/50) %>%
#                                        pull(Total)
#  })

## Se guardan las matrices de movilidad estudiantil para analizarlos después. 
wb <- createWorkbook()
for(i in 1:32){
     tabla <- tabla1[[i]] %>%
                as.data.frame() %>%
                 adorn_totals(c("row", "col"), 
                               fill = "-", 
                                na.rm = TRUE, 
          ,,,,contains(colnames(tabla1[[i]])))
                 
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, tabla, colNames = TRUE, rowNames = TRUE)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Matriz MEst nivel intermunicipal_Reduccion.xlsx"), 
               overwrite = TRUE)
}

saveRDS(tabla1, file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Tabla MEst a nivel intermunicipal.RDS"))
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Tabla MEst a nivel intermunicipal.RDS"))

total_tablas <- totales(tabla1 = tabla1, 
                        Clave = "CVE_MUN", 
                        Inmigrantes = "Salen por estudio",  
                        Emigrantes = "Entran por estudio")

porcentajes_tablas <- porcentajes(tabla1 = tabla1, 
                                  Clave = "CVE_MUN", 
                                  Inmigrantes = "%Salen por estudio",  
                                  Emigrantes = "%Entran por estudio")

# Se guardan los totales de las matrices reducidas 
wb <- createWorkbook()
for(i in 1:32){
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, total_tablas[[i]], colNames = TRUE, startCol = 1)
     writeData(wb, i, porcentajes_tablas[[i]], colNames = TRUE, startCol = 5)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Matriz MEst nivel intermunicipal_Reduccion_Totales.xlsx"), 
               overwrite = TRUE)
}

Dada la magnitud de municipios en algunos estados se seleccionan solo algunos de ellos.

tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Tabla MEst a nivel intermunicipal.RDS"))

# Paleta de colores
#paleta <- rev(colorRampPalette(wesanderson::wes_palette("Rushmore1"))(50)) 
paleta <- colorRampPalette(c("#000C7D", "#001599", "#0022B0", "#0035BB", "#004AB4", "#005EA3", "#00708D", "#078472","#3E9A85", "#49A980", "#58B877", "#70C669", "#94D25D", "#BBDA60", "#DBCE33", "#D6B92A", "#D1A521", "#CA911A"))(50)

tabla2 <- color_chord_diagram(tabla1 = tabla1, paleta)
file = "/Graficos/Municipio/04_Movilidad estudiantil/2015/ChordDiagram de MEst desagregado a nivel intermunicipal.pdf"

## Gráficos a nivel intermunicipal
chord_diagram_graph(file = file, 
                    width = 15, 
                    height = 10, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#000C7D",
                    transparency = 0,
                    circo.text = 9,
                    circos.axis.text = 6,
                    adj.text =c(-0.05, 0.5), #Ajuste de las etiquetas (x, y)
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))

Etiquetas

file = "/Graficos/Municipio/04_Movilidad estudiantil/2015/Etiquetas a nivel intermunicipal.pdf"

labels_chord_diagram(file = file, 
                     width = 7, 
                     height = 8, 
                     family = "Montserrat Medium", 
                     paleta = paleta, 
                     tabla1 = tabla1, 
                     labels = nom_estados)

Gráfico Sankey

# Matriz cuadrada a nivel municipal 
load(paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Matriz de movilidad estudiantil a nivel intermunicipal 2015.RData"))

tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character)

################################################################################
################################## Filtro ######################################
Inmigrantes  <- Migrantes %>%
                 as.data.frame() %>%
                  tibble::rownames_to_column(var = "rn") %>% 
                   melt(., id.vars = "rn", variable.name = "cn") %>%
                    mutate_if(is.factor, as.character) %>%
                     mutate(value = ifelse((.$rn != .$cn) & (substr(.$rn, 1, 3) %in% estados | substr(.$cn, 1, 3) %in% estados), value, 0)) %>%
                      filter(value > 0) %>%
                       group_by(rn) %>% 
                        summarise(Inmigrantes = sum(value, na.rm = TRUE)) 

Emigrantes <- Migrantes %>%
               as.data.frame() %>%
                tibble::rownames_to_column(var = "rn") %>% 
                 melt(., id.vars = "rn", variable.name = "cn") %>%
                  mutate_if(is.factor, as.character) %>%
                   mutate(value = ifelse((.$rn != .$cn) & (substr(.$rn, 1, 3) %in% estados | substr(.$cn, 1, 3) %in% estados), value, 0)) %>%
                    filter(value > 0) %>%
                     group_by(cn) %>% 
                      summarise(Emigrantes = sum(value, na.rm = TRUE))     

################################## Filtro ######################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE)) %>%
              filter(value < 5000) %>% 
               pull(rn)
################################################################################

tabla1 <- lapply(1:32, function(x){
                   tabla %>%
                    mutate(rn = case_when(substr(.$rn, 1, 3) %in% estados[x] & .$rn %in% filtro ~ paste0("Otros municipios (", estados[x], ")"),
                                          substr(.$rn, 1, 3) %in% estados[x]  & .$rn %nin% filtro ~ .$rn,
                                          substr(.$rn, 1, 3) %nin% estados[x] ~ substr(.$rn, 1, 3)),
                           cn = case_when(substr(.$cn, 1, 3) %in% estados[x] & .$cn %in% filtro ~ paste0("Otros municipios (", estados[x], ")"),
                                          substr(.$cn, 1, 3) %in% estados[x] & .$cn %nin% filtro ~ .$cn,
                                          substr(.$cn, 1, 3) %nin% estados[x] ~ substr(.$cn, 1, 3))) %>%

                     mutate(value = ifelse(.$rn != .$cn, .$value, 0)) %>%
                      filter(value > 0) 
  }
)
p <- lapply(1:32, function(x){
             tabla1[[x]] %>% 
              ggplot(aes(axis1 = rn, 
                          axis2 = cn, 
                           y = value),  # c("value", "freq", "tasa")
                      reverse = FALSE, 
                       na.rm = TRUE) +
               geom_alluvium(aes(fill = rn),
                              curve_type = "quintic", 
                               color = "transparent", 
                                alpha = 0.85, 
                                 lwd = 0.001, 
                                  width = 1/5,
                                   reverse = FALSE) +
                 geom_stratum(aes(fill = cn), 
                               color = "white", 
                                alpha = 0.65,  
                                 lwd = 0.001, 
                                  width = 1/5,
                                   reverse = FALSE) +
                  geom_text_repel(aes(label = ifelse(after_stat(x) == 1, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""), 
                                       fontface =  ifelse(after_stat(x) == 1, 'bold', 'plain')),
                                   stat = "stratum", 
                                    size = 3, 
                                     direction = "y", 
                                      nudge_x = -.2,
                                       min.segment.length = unit(1, "lines"),
                                        force = 1,
                                         force_pull = 0,
                                          family = "montserrat",
                                           reverse = FALSE) +
                   geom_text_repel(aes(label = ifelse(after_stat(x)  == 2, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                                        fontface =  ifelse(after_stat(x) == 2, 'bold', 'plain')),
                                    stat = "stratum", 
                                     size = 3,
                                      direction = "y", 
                                       nudge_x = .2, 
                                        force = 1,
                                         force_pull = 0,
                                          family = "montserrat",
                                           reverse = FALSE) +
                     theme_void() + 
                      theme(plot.margin = margin(t = 1, r = 5, b = 1, l = 0, "cm"),
                             text = element_text(family = "montserrat"),
                              axis.text = element_blank(),
                               axis.title = element_blank(),
                                strip.text = element_text(size = 10, face = "bold", family = "montserrat"),
                                 legend.key.size = unit(0.5, "cm"),
                                  legend.text = element_text(size = 7, family = "montserrat"),
                                   legend.position = c(1.02, .5)) + 
                       scale_x_discrete(expand = c(-0.1, 0.5)) +
                        scale_fill_viridis_d(option = "A", end = 1, begin = 0.2) +
                         guides(fill = guide_legend(ncol = 1, na.translate = F)) + 
                          labs(fill = "", 
                               color = "")
  }
)

path = paste0(here::here(), "/Graficos/Municipio/04_Movilidad estudiantil/2015/GSankey de MEst desagregado a nivel intermunicipal.pdf")
ggexport(list = p, width = 18, height = 10, dpi = 400, filename = path)

Indicadores

Se realizan cálculos generales de movilidad:

  • Residentes

  • Inmigrantes

  • Emigrantes

  • \(\%\) Inmigrantes

  • \(\%\) %Emigrante

  • Migración bruta

  • Migración Neta

  • \(\%\) Tasa de movilidad bruta

  • \(\%\) Tasa de movilidad neta

Se trabaja con la matriz cuadrada, la cual de esta manera no se satura computacionalmente

################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
              as.data.frame() %>%
               filter(CVE_MUN %in% municipios) %>%
                group_by(CVE_MUN) %>%
                 summarise(Pob.Total = sum(FACTOR)) 

################################################################################
###################### Pob. estudiantil de 3 años y más ########################
Pob.3ymas <- mydata %>%
              as.data.frame() %>%
               mutate(EDAD = as.numeric(.$EDAD)) %>%
                subset(EDAD >= 3 & EDAD <= 130) %>%
                 filter(CVE_MUN %in% municipios) %>%
                  group_by(CVE_MUN) %>%
                   summarise(Pob.3ymas = sum(FACTOR)) 

################################################################################
########################### Residentes #########################################
load(paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Matriz de movilidad estudiantil a nivel intermunicipal 2015.RData"))

Residentes <- Migrantes %>%
               rownames_to_column() %>%
                gather(CVE_MUN, Value ,-rowname)%>%
                 filter(rowname == CVE_MUN) %>%
                  select(-rowname) %>%
                   droplevels() %>%
                    rename("Residentes" = "Value") 

################################################################################
############################### Inmigrantes ####################################

## Población que sale de su entidad de residencia y entra a otro demarcación por motivos de estudios
Inmigrantes <- Migrantes %>% 
                as.data.frame() %>%
                 tibble::rownames_to_column(var = "CVE_MUN") %>%
                  melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_ASI") %>%
                   mutate_at(vars(3), as.numeric) %>%
                    as_tibble() %>%
                     filter(CVE_MUN != CVE_MUN_ASI) %>%
                      group_by(CVE_MUN) %>%
                       summarise(Inmigrantes = sum(value, na.rm = TRUE))

################################################################################
############################### Emigrantes #####################################

## Población que entra a la entidad para estudiar 
Emigrantes <- Migrantes %>% 
               as.data.frame() %>%
                tibble::rownames_to_column(var = "CVE_MUN") %>%
                 melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_ASI") %>%
                  mutate_at(vars(3), as.numeric) %>%
                   as_tibble() %>% 
                    filter(CVE_MUN != CVE_MUN_ASI) %>%
                     group_by(CVE_MUN_ASI) %>%
                      summarise(Emigrantes = sum(value, na.rm = TRUE)) %>%
                       rename("CVE_MUN" = "CVE_MUN_ASI") 

tabla <- Pob.Total %>%
          left_join(., Pob.3ymas, by = c("CVE_MUN")) %>%
          left_join(., Residentes, by = c("CVE_MUN")) %>%
          left_join(., Inmigrantes, by = c("CVE_MUN")) %>%
          left_join(., Emigrantes, by = c("CVE_MUN")) %>%
            mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
                   Mig.Bruta = .$Inmigrantes + .$Emigrantes, 
                   Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.3ymas) / 2)) * 1000,
                   Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.3ymas) / 2)) * 1000,
                   Tasa.Mig = Tasa.Inmig - Tasa.Emig, 
                   Eficacia = Mig.Neta - Mig.Bruta)

write.xlsx(tabla, file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Indicadores de movilidad estudiantil a nivel intermunicipal 2015.xlsx"), overwrite = TRUE)

save(tabla, file = paste0(here::here(), "/Output/Municipio/04_Movilidad estudiantil/2015/Indicadores de movilidad estudiantil a nivel intermunicipal 2015.RData"))
Indicadores de movilidad estudiantil
Nivel intermunicipal
CVE_MUN Pob.Total Pob.3ymas Residentes Inmigrantes Emigrantes Mig.Neta Mig.Bruta Tasa.Inmig Tasa.Emig Tasa.Mig Eficacia
001001 877 190 830 111 0 988 2 981 −1 993 3 969 0.2315 0.6984 −0.5 −5 962
001002 46 464 43 572 0 332 258 74 590 1.4750 1.1462 0.3 −516
001003 56 048 52 522 0 28 79 −51 107 0.1032 0.2911 −0.2 −158
001004 15 577 14 587 0 42 158 −116 200 0.5570 2.0952 −1.5 −316
001005 120 405 112 668 0 21 0 0 0 0.0360 0 0 0
001006 46 473 43 452 0 53 13 40 66 0.2358 0.0578 0.2 −26
001007 53 866 50 295 0 52 224 −172 276 0.1997 0.8602 −0.7 −448
001008 8 896 8 248 0 27 2 25 29 0.6300 0.0467 0.6 −4
001009 20 926 19 549 0 15 0 0 0 0.1482 0 0 0
001010 20 245 18 921 0 10 38 −28 48 0.1021 0.3881 −0.3 −76
001011 46 454 43 109 0 12 0 0 0 0.0536 0 0 0
002001 486 639 462 140 0 75 281 −206 356 0.0316 0.1185 −0.1 −562
002002 988 417 941 669 0 228 1 324 −1 096 1 552 0.0473 0.2744 −0.2 −2 648
002003 102 406 97 355 0 0 15 0 0 0 0.0300 0 0
002004 1 641 570 1 559 361 0 58 134 −76 192 0.0072 0.0167 0.0 −268
002005 96 734 91 779 0 4 0 0 0 0.0085 0 0 0
003001 72 564 69 201 0 40 0 0 0 0.1129 0 0 0
003002 60 171 56 803 0 119 0 0 0 0.4069 0 0 0
003003 272 711 260 109 0 39 83 −44 122 0.0293 0.0623 0.0 −166
003008 287 671 271 544 0 124 9 115 133 0.0887 0.0064 0.1 −18
Fuente: Estimaciones del CONAPO.

Referencias

Librerias que se usaron en el documento

package loadedversion source
Cairo 1.6-1 CRAN (R 4.3.1)
chorddiag 0.1.3 Github ()
circlize 0.4.15 CRAN (R 4.3.1)
doMC 1.3.5 R-Forge (R 4.3.1)
dplyr 1.1.3 CRAN (R 4.3.2)
expss 0.11.6 CRAN (R 4.3.1)
foreach 1.5.2 CRAN (R 4.3.1)
ggalluvial 0.12.5 CRAN (R 4.3.1)
ggplot2 3.4.3 CRAN (R 4.3.1)
ggpubr 0.6.0 CRAN (R 4.3.1)
ggrepel 0.9.3 CRAN (R 4.3.1)
ggsankey 0.0.99999 Github ()
gt 0.10.0 CRAN (R 4.3.1)
haven 2.5.3 CRAN (R 4.3.1)
Hmisc 5.1-0 CRAN (R 4.3.1)
iterators 1.0.14 CRAN (R 4.3.1)
janitor 2.2.0 CRAN (R 4.3.1)
kableExtra 1.3.4 CRAN (R 4.3.1)
knitr 1.45 CRAN (R 4.3.2)
maditr 0.8.3 CRAN (R 4.3.1)
mapview 2.11.0 CRAN (R 4.3.1)
Matrix 1.6-1.1 CRAN (R 4.3.1)
network 1.18.1 CRAN (R 4.3.1)
openxlsx 4.2.5.2 CRAN (R 4.3.1)
reshape2 1.4.4 CRAN (R 4.3.1)
sjlabelled 1.2.0 CRAN (R 4.3.1)
sna 2.7-1 CRAN (R 4.3.1)
srvyr 1.2.0 CRAN (R 4.3.1)
statnet.common 4.9.0 CRAN (R 4.3.1)
stringr 1.5.0 CRAN (R 4.3.1)
survey 4.2 Github ()
survival 3.5-5 CRAN (R 4.3.1)
tibble 3.2.1 CRAN (R 4.3.1)
tidyr 1.3.0 CRAN (R 4.3.1)

Creative Commons Licence
This work by Diana Villasana Ocampo is licensed under a Creative Commons Attribution 4.0 International License.